我在我的Android应用中使用Admob SMART_BANNER。当设备处于纵向模式时,应用程序将显示。但如果设备方向处于横向模式,则无法显示添加。我知道如果没有足够的空间来显示添加,则不会显示横幅添加。但在我的情况下,似乎有足够的空间来显示添加。这是我的代码:
activity_main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
values文件夹中的strings.xml文件:
<resources>
<string name="app_name">Banner Example</string>
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
</resources>
Java代码:
TelephonyManager tManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder`enter code here`()
.addTestDevice(uid)
.build();
mAdView.loadAd(adRequest);
答案 0 :(得分:1)
这可能不适用于原始问题,但我想分享我的解决方案 SMART_BANNER 不能正常工作。
我发现在我的情况下,SMART_BANNER想要采用屏幕的整个宽度,无论方向 - 横向还是纵向。我把AdView放在底部。如果它位于占据屏幕宽度80%的列中,它既不会显示也不会抱怨(即不调用onAdFailedToLoad)。它只是隐藏。一旦占据整个宽度,广告就会毫无问题地显示出来。
答案 1 :(得分:0)
try this below code
private void showBannerAd()
{
AdView adView = new AdView(HomeActivity.this);
//cal method to show banner add
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
adView.setAdSize(AdSize.BANNER);
}
else
{
adView.setAdSize(AdSize.SMART_BANNER);
}
adView.setAdUnitId("AD_ID");
AdRequest adRequest = new
AdRequest.Builder().addTestDevice(Secure.getString(getContentResolver()
,Secure.ANDROID_ID)).build();
adView.loadAd(adRequest);
}
答案 2 :(得分:0)
您可以根据设备动态加载adsize&#39;屏幕分辨率:
AdSize adSize = AdSize.SMART_BANNER;
DisplayMetrics dm = getResources().getDisplayMetrics();
double density = dm.density * 160;
double x = Math.pow(dm.widthPixels / density, 2);
double y = Math.pow(dm.heightPixels / density, 2);
double screenInches = Math.sqrt(x + y);
if (screenInches > 8) { // > 728 X 90
adSize = AdSize.LEADERBOARD;
} else if (screenInches > 6) { // > 468 X 60
adSize = AdSize.MEDIUM_RECTANGLE;
} else { // > 320 X 50
adSize = AdSize.BANNER;
}
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdUnitId(yourAdUnitHere);
mAdView.setAdSize(adSize);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);