【问题标题】:How can I place an AdView above a preference fragment?如何将 AdView 放在偏好片段上方?
【发布时间】:2015-03-14 23:04:44
【问题描述】:

我有一个包含偏好片段的设置活动。我想在这个片段上方放置一个 adView(横幅广告)。

到目前为止,我设法显示了广告横幅,但横幅显示在片段后面(我的片段具有透明背景,所以我看到了广告,但它被偏好片段的文本覆盖)。

如何将广告横幅放置在片段前面?非常感谢任何帮助!谢谢。

这是我在设置活动中的代码:

public class Settings extends Activity  {

public static PrefsFragment mPrefsFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       

    setContentView(R.layout.settings_layout);

    if (savedInstanceState == null) {
        mPrefsFragment = new PrefsFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(android.R.id.content, mPrefsFragment).commit();
    }

}

@Override
public void onResume() {

    super.onResume();

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().addTestDevice("1234").build();
    mAdView.loadAd(adRequest);

}

[......]

}

还有我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >


    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

【问题讨论】:

  • 对于漂亮的 UI 设计,我不建议您将广告放在设置或首选项中。
  • 我正在开发一个动态壁纸,它有一个小的免费测试版和一个完整的无广告版本。偏好窗口实际上是此应用程序中唯一的广告位置。

标签: android layout fragment adview banner-ads


【解决方案1】:

在欢迎偏好的片段中,你可以做这样的事情。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    LinearLayout v = (LinearLayout) super.onCreateView(inflater, container, savedInstanceState);
    AdView adView = new AdView(getContext());
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    adView.setAdSize(AdSize.BANNER);


    AdRequest request1 = new AdRequest.Builder().build();
    adView.loadAd(request1);
    v.addView(adView);
    return v;
}

【讨论】:

  • 这个解决方案对我有用 PreferenceFragmentCompat 谢谢,伙计。
猜你喜欢
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多