代码之家  ›  专栏  ›  技术社区  ›  TommyTomato

Admob横幅广告不显示

  •  0
  • TommyTomato  · 技术社区  · 7 年前

    我没有发现错误,只是没有显示出来。

    <uses-permission android:name="android.permission.INTERNET" android:maxSdkVersion="28"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission-sdk-23 android:name="android.permission.INTERNET"
        android:maxSdkVersion="28" />
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_NETWORK_STATE"
        android:maxSdkVersion="28"/>
    

    在布局中,我有:

    <com.google.android.gms.ads.AdView
        android:id="@+id/publisherAdView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-??????????????/????????"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    

    在活动中,我有:

    private AdView mPublisherAdView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_normal);
        MobileAds.initialize(this, "ca-app-pub-?????????????~???????");
    
        mPublisherAdView = findViewById(R.id.publisherAdView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mPublisherAdView.loadAd(adRequest);
    

    这是我的graddle文件:

    buildscript {
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    
    
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
    }
    
    allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    }
    task clean(type: Delete) {
    delete rootProject.buildDir
    }
    

    以及:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "???????.?????"
            minSdkVersion 17
            targetSdkVersion 26
            versionCode 1203
            versionName "1.2.03"
            testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    //GSON
    implementation 'com.google.code.gson:gson:2.8.2'
    //RecycleView
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    //ADS
    implementation 'com.google.android.gms:play-services-ads:17.1.1'
    }
    

    2 回复  |  直到 7 年前
        1
  •  0
  •   Raza    7 年前

    addRequest 正确使用 AdListener

    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
          if (mAdView.getVisibility() == View.GONE) {
                mAdView.setVisibility(View.VISIBLE);
           }
         }
      });
    mAdView.loadAd(adRequest);
    

    Adview的XML是

    <LinearLayout
        android:id="@+id/ad_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        >
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            android:visibility="gone"
            ads:adUnitId="@string/banner_ad_id">
        </com.google.android.gms.ads.AdView>
    </LinearLayout>
    

    banner id 在字符串文件中。

        2
  •  0
  •   TommyTomato    7 年前