2017-10-08 1 views
0

Ich habe eine Frage.Admob ID von Aktivität

Ich habe Admob Banner zu meiner app,

es gut funktioniert.

Der Code:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Load an ad into the AdMob banner view. 
    AdView adView = (AdView) findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder() 
      .setRequestAgent("android_studio:ad_template").build(); 
    adView.loadAd(adRequest); 
} 

xml:

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

Ich möchte Anzeigen ändern: adUnitId von mainactivity.

ein Beispiel, wenn eine == 1 Anzeigen: adUnitId = xxxxxxx sonst Anzeigen: adUnitId = yyyyyyy

Wie kann ich das tun?

Danke.

Edit: AndroidManifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="demo.com.bannertest"> 

    <!-- Include required permissions for Google Mobile Ads to run. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <!-- This meta-data tag is required to use Google Play Services. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> <!-- Include the AdActivity configChanges and theme. --> 
     <activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 
    </application> 

</manifest> 

mainactivity:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Load an ad into the AdMob banner view. 
     AdView adView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder() 
       .setRequestAgent("android_studio:ad_template").build(); 
     adView .setAdUnitId("ca-app-pub-3940256099942544/6300978111"); 
     adView.loadAd(adRequest); 
    } 
} 

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <!-- view for AdMob Banner Ad --> 
    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     ads:adSize="BANNER" /> 

</RelativeLayout> 

logcat: Erforderliche XML-Attribut "adUnitId" fehlte.

Antwort

0

erstellen Sie das AdView pragmatisch mit diesem Code.

View adContainer = findViewById(R.id.adMobView); 
    AdView mAdView = new AdView(context); 
    mAdView.setAdSize(AdSize.BANNER); 
    mAdView.setAdUnitId(YOUR_BANNER_ID); 
    ((RelativeLayout)adContainer).addView(mAdView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    mAdView.loadAd(adRequest); 

Und verwenden Sie diesen Code in Ihre XML-Datei

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/adMobView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentBottom="true"/> 

</RelativeLayout> 
+0

adView .setAdUnitId (banner_id); nicht arbeiten. – michaelzX

+0

Ihre Anzeigen funktionieren einwandfrei, setzen Sie die adUnitId in der XML-Datei? –

+0

ja, es funktioniert gut. – michaelzX

Verwandte Themen