Android:WindowManager视图计数

时间:2014-02-19 14:12:58

标签: android android-windowmanager

在我的应用程序中,我向窗口添加了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();
}

上面是我用来添加所有需要的视图的函数

1 个答案:

答案 0 :(得分:0)

简单的解决方案:

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

额外职业选手:你自己不需要做“MarginTop”!

我在Play商店中将此方法用于我的应用程序Tinycore