简体   繁体   English

如何使用 Java 设置 AdUnitId?

[英]How can I set AdUnitId using Java?

Code in Java: Java中的代码:

        mAdView = (AdView)findViewById(R.id.adView);
        mAdView.setAdSize(AdSize.BANNER);
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

View in XML (I'm using Constraint Layout):以 XML 格式查看(我使用的是约束布局):

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn"
        app:layout_constraintVertical_bias="1.0" />

The result:结果:

在此处输入图片说明

How can I fix this?我怎样才能解决这个问题?

I don't want to use "ads:adUnitId="ca-app-pub-3940256099942544/6300978111" in XML. I need to set AdUnitId in Java.我不想在 XML 中使用 "ads:adUnitId="ca-app-pub-3940256099942544/6300978111"。我需要在 Java 中设置 AdUnitId。

It is easier to include in XML else you have to remove from XML and build everything programmatically , even set position of banner container layout etc ..包含在 XML 中更容易,否则您必须从 XML 中删除并以编程方式构建所有内容,甚至设置横幅容器布局的位置等。

   public class MyActivity extends Activity {
     private AdView mAdView;

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

         LinearLayout layout = new LinearLayout(this);
         layout.setOrientation(LinearLayout.VERTICAL);

         // Create a banner ad. The ad size and ad unit ID must be set before calling loadAd.
         mAdView = new AdView(this);
         mAdView.setAdSize(AdSize.SMART_BANNER);
         mAdView.setAdUnitId("myAdUnitId");

         // Create an ad request.
         AdRequest.Builder adRequestBuilder = new AdRequest.Builder();

         // Optionally populate the ad request builder.
         adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);

         // Add the AdView to the view hierarchy.
         layout.addView(mAdView);

         // Start loading the ad.
         mAdView.loadAd(adRequestBuilder.build());

         setContentView(layout);
     }

     @Override
     public void onResume() {
         super.onResume();

         // Resume the AdView.
         mAdView.resume();
     }

     @Override
     public void onPause() {
         // Pause the AdView.
         mAdView.pause();

         super.onPause();
     }

     @Override
     public void onDestroy() {
         // Destroy the AdView.
         mAdView.destroy();

         super.onDestroy();
     }
 }

https://developers.google.com/android/reference/com/google/android/gms/ads/AdView https://developers.google.com/android/reference/com/google/android/gms/ads/AdView

****** update ****** 更新

I think this will fit with what you need我认为这将符合您的需要

1- Replace banner in xml with LinearLayout 1- 用 LinearLayout 替换 xml 中的横幅

2- make id= layout_banner_holder 2- 使 id= layout_banner_holder

3- use this method for implement banner in it 3-使用此方法在其中实现横幅

private void showBannerAds() {

        LinearLayout adBannerLayout = (LinearLayout) findViewById(R.id.layout_banner_holder);

        adView = new AdView(this);
        adView.setAdUnitId(ca-app-pub-3940256099942544/6300978111);
        adView.setAdSize(AdSize.SMART_BANNER);
        adBannerLayout.addView(adView);

        AdRequest adRequest;

            adRequest = new AdRequest.Builder().build();

        adView.loadAd(adRequest);

    }

4- call it in oncreate 4-在oncreate中调用它

showBannerAds();
    <LinearLayout
        android:id="@+id/layout_banner_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#838383"
        android:orientation="vertical"
        android:visibility="visible"></LinearLayout>



    LinearLayout adBannerLayout = (LinearLayout) findViewById(R.id.layout_banner_holder);

    AdView adView = new AdView(this);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    adView.setAdSize(AdSize.SMART_BANNER);
    adBannerLayout.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM