为什么横幅广告无法以编程方式在相对布局中展示?
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
tools:context=".HomeActivity">
</RelativeLayout >
Java
void loadAds() {
if (checkInternet.isConnectingToInternet()) {
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Test").child("Ads");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Banner = dataSnapshot.child("Banner").getValue().toString();
Interstitial = dataSnapshot.child("Interstitial").getValue().toString();
Rewarded = dataSnapshot.child("Rewarded").getValue().toString();
InterstitialPage = dataSnapshot.child("InterstitialPage").getValue().toString();
AdView mAdView = new AdView(HomeActivity.this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId(Banner);
relative_layout.addView(mAdView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
希望您能帮助我找到解决方案。
答案 0 :(得分:4)
尝试将adView放在xml的相对布局中,如下所示:
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
,然后将ca-app-pub-3940256099942544 / 6300978111更改为adUnitId。 还要检查this
希望对您有帮助
答案 1 :(得分:0)
尝试使用测试广告,如果有效,则问题出在您的广告号码上
ca-app-pub-3940256099942544/6300978111