这是一个更简单的解决方案:创建一个显示单个广告的新首选项类型.然后,您可以在首选项的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布局:
xmlns:myapp="http://schemas.android.com/apk/res/"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
android:layout_height="wrap_content" myapp:backgroundColor="#000000" myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC" />
然后你只需在你的首选项xml定义中添加这样的东西:
android:orderingFromXml="true">
... other preferences ...