所有首选屏幕上的Android Admob横幅广告

时间:2014-10-14 11:03:20

标签: android layout admob settings preferencescreen

我在设置中放置AdMob横幅时遇到问题。我已设法通过使用以下布局将其放在第一个屏幕中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MainLinearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:baselineAligned="false">

   <fragment
       android:id="@+id/admob"
       android:name="ro.infiniteloop.phonehancer.AdmobFragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

    <fragment
        android:id="@+id/settings"
        android:name="ro.infiniteloop.phonehancer.SettingsFragment"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

</LinearLayout>

AdmobFragment布局为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/AdmobLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ro.infiniteloop.phonehancer.AdmobFragment" >

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-5599997006266549/8381977716" />

</LinearLayout>

设置片段布局:

<PreferenceCategory
    android:key="pref_key_storage_settings"
    android:title="@string/pref_sms_storage_title" >

    <CheckBoxPreference
        android:defaultValue="false"
        android:key="pref_key_auto_delete"
        android:summary="@string/pref_summary_auto_delete"
        android:title="@string/pref_title_auto_delete" />

.....

An image of the output

到目前为止一切顺利。但是当我进入儿童偏好设置屏幕时,原始布局会重置,广告将不再显示

An image of the child preference screen

有没有办法在布局中放置这些元素或编写一些自定义代码,使广告保留在所有首选项子屏幕上?

1 个答案:

答案 0 :(得分:0)

创建仅包含admob的布局

admob.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/AdmobLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-5599997006266549/8381977716" />

现在在设置片段中添加此视图。

  getActivity().setcontentView(R.layout.admob);

您的广告将可见