android - 如何将 AdMob 横幅放在高度为 MATCH_PARENT 的 View 下方?

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

我有一个 ViewPager,它的 PagerTitleStrip 的高度为 MATCH_PARENT。页面只有一段文字,但有些页面因为文字很长,所以有垂直滚动。

现在,我正在尝试寻找将 AdMob 横幅广告放在布局底部的最佳策略。是这样的情况: - 如果我放在 ViewPager 下方,则横幅不可见,因为 ViewPager 具有 MATCH_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>

最佳答案

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

<?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>

关于android - 如何将 AdMob 横幅放在高度为 MATCH_PARENT 的 View 下方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954156/

相关文章:

Android - 与 android :id 具有不同 ID 的 fragment

Android - fill_parent 在relativelayout 中的行为不符合预期

android - Phonegap 安卓权限

android - Trigger.io barcode.scan 抛出 Java 错误

android - 在android中显示对齐的长段落?

Android Activity 指示器?

android - 如何使用express编写recyclerview拖放的测试用例

具有多个图像淡入淡出动画的android imageview

Android - 从图库应用程序获取图像?

android - 带有嵌套 RelativeLayout 的 SwipeRefreshLayout