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();
}

выше это функция, которую я использовал для добавления всех необходимых представлений


person xXJJJasonMokXx    schedule 19.02.2014    source источник


Ответы (1)


Простое решение:

Добавьте один глобальный LinearLayout и добавьте в него баннеры, у LinearLayout есть метод getChildCount().

Дополнительные плюсы: Вам не нужно делать «MarginTop» самостоятельно!

Я использую этот метод для своего приложения Tinycore в Play Store.

person Rolf ツ    schedule 19.02.2014
comment
На самом деле у меня уже есть LinearLayout, сидящий позади RelativeLayout, которые являются "banners", но тогда они все еще перекрывают друг друга. Есть идеи? - person xXJJJasonMokXx; 19.02.2014
comment
Добавляете ли вы RelativeLayouts с помощью метода addView? - person Rolf ツ; 19.02.2014
comment
Да. С LayoutParams тоже - person xXJJJasonMokXx; 19.02.2014
comment
Вы должны опубликовать часть своего кода! Какие виды вы добавляете в WindowManager? - person Rolf ツ; 19.02.2014