android - ViewPager 中标签上方的 AdMob 横幅

标签 android android-layout android-fragments android-viewpager admob

enter image description here

如图所示,目前我的 ViewPager TabPager 下方有一个 AdMob 横幅,我希望横幅位于 ViewPager 上方。我以编程方式添加横幅,并将 ViewPager 设置为 RelativeLayout 中的唯一元素。

activity_main.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >


<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="wrap_content" 
    android:layout_weight="1">
    </android.support.v4.view.ViewPager>

</RelativeLayout>

在activity_main.xml 中将Admob Banner 添加到RelativeLayout

adView = new AdView(this, AdSize.SMART_BANNER, "ADMOB_ID");

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, R.id.layout);

layout.addView(adView, params);

最佳答案

默认选项卡布局附加到操作栏。如果您想在选项卡上方显示广告,最好在 xml 布局中使用 TabHost。

例如:

  • 布局
    • 广告 View
    • 标签
    • 内容(寻呼机)

编辑:查看我在评论中链接的视频

你的 xml 应该看起来像这样

`

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1Name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

/*

TAB 1 内容在这里

*/

            <LinearLayout
                android:id="@+id/tab2Name"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

/*

此处为 TAB 2 内容

*/

            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>`

关于android - ViewPager 中标签上方的 AdMob 横幅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308726/

相关文章:

java - 深度 sleep 连接蓝牙设备失败

java - 在后台持续检查互联网 - Android

Android - RecyclerView StaggeredGridLayoutManager 最后一行的拉伸(stretch)宽度

android - 将一个类绑定(bind)到两个布局?

android - 如何仅在某些 fragment 中启用 tablayout?

android - 无法通过 Bundle 将数据传递给 fragment

java - 如何等待 Intent 服务完成 - 当 Intent 服务从 AsyncTask 启动时

android libuv 使用未声明的标识符 'EPOLL_CLOEXEC'

Android Oreo 字体系列 NPE 崩溃

android - 使用 CoordinatorLayout 在 fragment 中隐藏 Activity 中的工具栏