如图所示,目前我的 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/