我怎么把一个冬季圣诞雪景的广告平台在设置视为一个活生生的墙纸?

StackOverflow https://stackoverflow.com/questions/4003701

  •  25-09-2019
  •  | 
  •  

我看见马里奥壁纸使用的新版中设置屏幕,但我不能做我自己。如果我把广告平台进入settings.xml 像你会与一个正常的布局我得到一个类投例外。

这里的截图马里奥壁纸说什么,我试图做到的。

example screenshot

有帮助吗?

解决方案

下面是一个简单的解决方案:创建一个新的偏好型显示单个广告。然后,您可以在您的喜好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);
    }

}

XML布局AdmobPreference:

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

其他提示

我已经成功地回答这个对于我自己,所以我会在情况下,别人有同样的问题。

张贴在这里的解决方案

我添加了一个TabActivity以及标准首活动,则嵌套在TabActivity的选项卡内的首选项。这意味着我有用于TabActivity,我可以把一个AD浏览(或任何其他类型的视图)中的普通布局的xml内部和我还有所产生的首选项屏幕内的工作。

代码为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>

设置机器人:能见度=“隐形”和机器人:layout_height =在TabWidget标签装置“1DP”用户不能告诉它实际上是一个标签

感谢你,我真的很期待这一点。

只是一个警告:如果你使用这种方法生产的代码和你使用ProGuard你需要告诉ProGuard的不编码类AdmobPreference,否则将充气机FC

这行添加到您的proguard.cfg:

-keep public class * extends android.preference.Preference

对于那些想知道如何使用PreferenceFragment实现这一点,这里是一个解决方案,也没有必要创建一个自定义Preference

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

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

不要忘了使用谷歌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();  
    }

你可以实现一个屏幕设置在两个方面:

  1. 使用偏好的框架,以便在XML文件的偏好自动造成偏好的布局和它的所有功能
  2. 写自己的活动,即上装载显示当前偏好,因为它们显示,它们保存,使用一个正常的布局XML文件的创建

我怀疑你是增加对冬季圣诞雪景象XML文件,该文件无法处理它,可以onlky处理优先项目。请你XML文件,并指定哪铸造的错误你都看到的。

如果你想要最终控制权的内容偏好,屏幕,然后执行它自己作为一个正常的活动,然后你可以做任何你想要的。

    @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);
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top