【问题标题】:How do I put an admob adview in the settings screen for a live wallpaper?如何在动态壁纸的设置屏幕中放置 admob adview?
【发布时间】:2016-11-04 05:30:50
【问题描述】:

我已经看到马里奥动态壁纸在设置屏幕中使用 admob 广告,但我自己无法做到。如果我像使用普通布局一样将 adview 放入 settings.xml 中,我会得到一个类转换异常。

这是马里奥动态壁纸的屏幕截图,以说明我正在尝试做的事情。

【问题讨论】:

    标签: android layout admob


    【解决方案1】:

    这里有一个更简单的解决方案:创建一个显示单个广告的新偏好类型。然后,您可以在 xml 定义中包含该偏好类型,以便您的偏好显示一个或多个广告。

    自定义偏好类:

    public class AdmobPreference extends Preference
    {
    
        public AdmobPreference(Context context) {
            super(context, null);
        }
    
        public AdmobPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected View onCreateView(ViewGroup parent) {
                //override here to return the admob ad instead of a regular preference display
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return inflater.inflate(R.layout.admob_preference, null);
        }
    
    }
    

    AdmobPreference 的 XML 布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myapp="http://schemas.android.com/apk/res/<YOUR PACKAGE>"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
    >
    
        <com.google.ads.AdView android:id="@+id/ad" android:layout_width="fill_parent"
            android:layout_height="wrap_content" myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
            myapp:secondaryTextColor="#CCCCCC" />
    </LinearLayout>
    

    然后您只需将类似这样的内容添加到您的首选项 xml 定义中:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orderingFromXml="true">
    
        <YOUR PACKAGE NAME.AdmobPreference android:key="ad" />
    
        ... other preferences ...
    </PreferenceScreen>
    

    【讨论】:

    • 看起来这会使广告与偏好列表的其余部分一起滚动。
    【解决方案2】:

    我已经自己回答了这个问题,所以我会在这里发布解决方案,以防其他人有同样的问题。

    我添加了一个 TabActivity 以及标准的 Preferences 活动,然后将 Preferences 嵌套在 TabActivity 的 Tab 中。这意味着我有一个用于 TabActivity 的普通布局 xml,我可以在其中放置一个 adview(或任何其他类型的视图),并且我仍然可以在其中运行生成的首选项屏幕。

    TabActivity 的代码

    public class SettingsTabActivity extends TabActivity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tablayout);
    
            TabHost tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Resusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab
    
            // Create an Intent for the regular live wallpaper preferences activity
            intent = new Intent().setClass(this, Preferences.class);
    
            // Initialize a TabSpec and set the intent
            spec = tabHost.newTabSpec("TabTitle").setContent(intent);
            spec.setIndicator("TabTitle");
    
            tabHost.addTab(spec);
    
            tabHost.setCurrentTab(0);      
        }
    }
    

    tablayout.xml 的代码

    <?xml version="1.0" encoding="utf-8"?>
    
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myapp="http://schemas.android.com/apk/res/*your package name goes here for admob*"
        android:id="@android:id/tabhost" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent" android:layout_height="fill_parent">
    
                <com.admob.android.ads.AdView android:id="@+id/ad"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
                        myapp:secondaryTextColor="#CCCCCC" />
    
                <TabWidget android:id="@android:id/tabs"
                        android:layout_width="fill_parent" android:layout_height="1dp"
                        android:tabStripEnabled="false" android:visibility="invisible" />
    
                <FrameLayout android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent" android:layout_height="fill_parent"
                        android:padding="1dp" />
        </LinearLayout>
    </TabHost>
    

    在 TabWidget 标签上设置 android:visibility="invisible" 和 android:layout_height="1dp" 意味着用户无法判断它实际上是一个 Tab

    【讨论】:

    【解决方案3】:

    谢谢你,我真的在找这个。

    只是一个警告:如果您将这种方法用于生产代码并且您正在使用 proguard,您需要告诉 proguard 不要对 AdmobPreference 类进行编码,否则充气器将 FC。

    将此行添加到您的 proguard.cfg:

    -keep public class * extends android.preference.Preference
    

    【讨论】:

      【解决方案4】:

      对于那些想知道如何使用PreferenceFragment 实现此功能的人,这里有一个解决方案,无需创建自定义Preference

      首先在PreferenceFragment中创建一个新的布局(这里在布局资源文件夹中命名为settings.xml):

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:layout_height="match_parent"
                      android:layout_width="match_parent">
      
          <com.google.android.gms.ads.AdView
                  xmlns:ads="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/adview"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  ads:adUnitId="XXXXX"
                  ads:adSize="SMART_BANNER"
                  android:layout_alignParentTop="true" />
      
          <ListView android:id="@android:id/list" 
              android:layout_width="match_parent" 
              android:layout_height="wrap_content"
              android:layout_below="@id/adview" />
      
      </RelativeLayout>
      

      然后,在PreferenceFragment 中使用这个布局,在onCreateView 方法中加载它:

      private AdView adView;
      @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View v = inflater.inflate(R.layout.settings, null);
      
              adView = (AdView)v.findViewById(R.id.adview);
      
              //** Adview */
              if(Tools.isNetworkConnected(getActivity())){
                  adView.setVisibility(View.VISIBLE);
              }
              else{
                  adView.setVisibility(View.GONE);
              }
      
              //Create ad banner request
              AdRequest adBannerRequest = new AdRequest.Builder()
      //      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
      //      .addTestDevice("XXXXX")
              .build();
      
              // Begin loading banner
              adView.loadAd(adBannerRequest);
      
              return v;
          }
      

      别忘了为使用 Google Play 服务的新版 Admob 添加以下代码:

      @Override
          public void onResume() {
              super.onResume();
              if(adView != null){
                  adView.resume();
              }
          }
      
          @Override
          public void onPause() {
              if(adView != null){
                  adView.pause();
              }
              super.onPause();
          }
      
          @Override
          public void onDestroy() {
              if(adView != null){
                  adView.destroy();
              }
              super.onDestroy();  
          }
      

      【讨论】:

      • 创建自定义偏好确实可以让您轻松地在设置/其他偏好之间插入广告。而您的解决方案只允许在设置的底部或顶部放置广告。
      • 是的,我同意,但问题是寻求将广告放在顶部的解决方案,这就是我的代码正在做的事情。
      【解决方案5】:

      您可以通过两种方式实现设置屏幕:

      1. 使用首选项框架,以便 XML 首选项文件自动创建首选项布局及其所有功能
      2. 自己编写活动,即在加载时显示当前首选项,如显示的那样,保存它们,使用您创建的普通布局 XML 文件

      我怀疑您将 AdMob 对象添加到无法处理它的 XML 文件中,并且只能处理首选项。请发布您的 XML 文件,并说明您看到的转换错误。

      如果您想对首选项屏幕的内容进行最终控制,请将其作为正常活动自己实现,然后您可以做任何您想做的事情。

      【讨论】:

        【解决方案6】:
            @Override
            public void onViewCreated(View view, Bundle savedInstanceState) {
                ViewGroup linear = (ViewGroup) view;
                while (!(linear instanceof LinearLayout))
                    linear = (ViewGroup) linear.getParent();
                AdView adView = new AdView();
                linear.addView(adFrame);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-16
          • 1970-01-01
          • 1970-01-01
          • 2012-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多