【问题标题】:How to put a AdMob banner below a view with MATCH_PARENT height?如何将 AdMob 横幅放在具有 MATCH_PARENT 高度的视图下方?
【发布时间】:2016-08-15 11:24:07
【问题描述】:

我有一个ViewPager 和一个PagerTitleStrip,其高度为MATCH_PARENT。页面只有一个文本,但有些页面有垂直滚动,因为文本很长。

现在我正在尝试寻找将 AdMob 横幅放在布局底部的最佳策略。这是这种情况: - 如果我放在ViewPager 下方,则横幅不可见,因为ViewPagerMATCH_PARENT。 - 如果我放在布局的底部,那么ViewPager 正在从横幅后面滚动内容,这不是正确的行为。

是否可以设置一条规则,使横幅保持在ViewPager 下方,但不使ViewPager 的滚动内容位于横幅后面?

谢谢

我尝试添加横幅的方式:

RelativeLayout.LayoutParams adsParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        adsParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adsParams.addRule(RelativeLayout.BELOW,R.id.pager);
        if (AdManager.getInstance().getAdView()!=null){
            View view = AdManager.getInstance().getAdView();
            mainLayout.addView(view, adsParams);
        }

我的布局:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <RelativeLayout
        android:id="@+id/main_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.PagerTitleStrip
                android:id="@+id/pager_title_strip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:paddingTop="4dp"
                android:paddingBottom="4dp"
                style="@style/CustomPagerTitleStrip"/>
        </android.support.v4.view.ViewPager>
    </RelativeLayout>
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"
        style="@style/NavigationDrawerStyle"/>
</android.support.v4.widget.DrawerLayout>

【问题讨论】:

  • 您不能将ViewPager 和横幅包装在一个布局中,比如LinearLayout,然后将它们堆叠在一起吗?这样您就不会有任何内容重叠。

标签: android android-layout android-relativelayout android-layoutparams


【解决方案1】:

在您的布局 XML res 文件中,在 MATCH_PARENT 视图中添加此属性 android:layout_above。并在其值中给出广告横幅的 id。现在您的 match_parent 视图将位于广告横幅上方

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_above="@+id/temp"
        android:layout_height="match_parent"></RelativeLayout>
    <View
        android:id="@+id/temp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="60dp"/>

</RelativeLayout>

【讨论】:

  • 为什么我把它放在上面,而如果我把它放在横幅视图下面却不起作用?令人困惑
  • Layout_above 意味着这个 match_parent 视图应该在指定的广告横幅视图之上。
  • 是的,但是 layout_below 是相反的,如果我放在横幅视图上应该可以工作,这是必须低于另一个的视图。不要工作。为什么?
  • 不,它不会像 MATCH_PARENT 一样工作,这意味着这个视图应该填满整个父屏幕,当你把 adbanner 作为 layout_below 时,它会在设备屏幕下方。所以它不会出现
  • 您实际上可以在android studio中xml布局文件的预览窗口或设计选项卡中看到所有可能的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 2017-01-25
  • 1970-01-01
相关资源
最近更新 更多