gpt4 book ai didi

android - 如何在动态壁纸的设置屏幕中放置 admob adview?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:26 27 4
gpt4 key购买 nike

我在设置界面看到马里奥动态壁纸使用admob广告,但我自己没能做到。如果我像使用普通布局一样将 adview 放入 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);
}

}

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>

关于android - 如何在动态壁纸的设置屏幕中放置 admob adview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4003701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com