Android:窗口管理器的浏览量很重要



在我的应用程序中,我向窗口添加了一个RelativeLayout(它们是横幅排序)。有时可能会有多个"banner",因此我想计算窗口内"banners"的视图数量,以便我可以设置一些MarginTop或排序,以免它们相互重叠。知道吗?

private void initWindow() {
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mainNotificationFrameContainer = new LinearLayout(this);
    mainNotificationFrame = new RelativeLayout(this);
    notificationDisplay = new TextView(this);
    notificationIcon = new ImageView(this);
    closeBannerButton = new ImageView(this);
    fadeInNotificationBanner = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in_notification_banner);
    fadeOutNotificationBanner = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out_notification_banner);
    setUpViews();
    final WindowManager.LayoutParams mainNotificationFrameContainerLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );
    mainNotificationFrameContainerLayoutParams.gravity = Gravity.TOP;
    mainNotificationFrameContainerLayoutParams.y = 0;
    final WindowManager.LayoutParams mainNotificationFrameLayoutParams = new WindowManager.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            150,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );
    final WindowManager.LayoutParams notificationIconLayoutParams = new WindowManager.LayoutParams(
            150,
            150,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
    );
    final RelativeLayout.LayoutParams closeNotificationBannerLayoutParams = new RelativeLayout.LayoutParams(
            150,
            150
    );
    closeNotificationBannerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    final RelativeLayout.LayoutParams notificationDisplayLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );
    notificationDisplayLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.notificationId);
    windowManager.addView(mainNotificationFrameContainer, mainNotificationFrameContainerLayoutParams);
    mainNotificationFrameContainer.addView(mainNotificationFrame, mainNotificationFrameLayoutParams);
    mainNotificationFrame.startAnimation(fadeInNotificationBanner);
    mainNotificationFrame.addView(notificationIcon, notificationIconLayoutParams);
    mainNotificationFrame.addView(notificationDisplay, notificationDisplayLayoutParams);
    mainNotificationFrame.addView(closeBannerButton, closeNotificationBannerLayoutParams);
    setUpListeners();
}

上面是我用来添加所有需要的视图的功能

简单的解决方案:

添加一个全局LinearLayout并添加其中的横幅,LinearLayout有一个方法getChildCount()

额外的优点:你不需要自己做一个"保证金顶部"!

我在Play商店中为我的应用程序Tinycore使用此方法

最新更新