Adview 大小 Smart_Banner 在更改方向时损坏
Adview Size Smart_Banner broke when change orientation
我遇到了一个问题,当我改变方向屏幕时,smart_banner 没有刷新。
我搜索了一下 smart_banner 应该会自动刷新横幅的大小,但他们没有这样做,我不明白为什么。
编辑:我如何解决问题
我只是通过 onConfigurationChanged() 上的代码删除、重新绑定和重新添加我的 Adview:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Remove the ad keeping the attributes
AdView ad = (AdView) findViewById(R.id.ad_view_home);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) (ad != null ? ad.getLayoutParams() : null);
RelativeLayout parentLayout = (RelativeLayout) (ad != null ? ad.getParent() : null);
if (parentLayout != null) {
parentLayout.removeView(ad);
}
// Re-initialise the ad
mAdView.destroy();
mAdView = new AdView(this);
mAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id_home));
mAdView.setId(R.id.ad_view_home);
mAdView.setLayoutParams(lp);
if (parentLayout != null) {
parentLayout.addView(mAdView);
}
// Re-fetch add and check successful load
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.device_id_test1))
.build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Utils.setLayoutVisible(mAdView);
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Utils.setLayoutInvisible(mAdView);
}
});
}
AndroidManifest.xml:
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
我正在 XML 中设置广告视图:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="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="br.com.adley.myseriesproject.activities.HomeActivity">
<content here...>
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id_search_show"/>
</RelativeLayout>
在活动中,我这样设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
activateToolbarWithNavigationView(HomeActivity.this);
//Ad Config
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, getString(R.string.application_id_ad));
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) findViewById(R.id.ad_view_home);
// Create an ad request. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
mAdRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.device_id_test1))
.build();
// Start loading the ad in the background.
mAdView.loadAd(mAdRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Utils.setLayoutVisible(mAdView);
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Utils.setLayoutInvisible(mAdView);
}
});
// Tabs Setup
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.home_pager);
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.favorites_label_fragment)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.air_today_label_fragment)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final HomePageAdapter adapter = new HomePageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
if (viewPager != null) {
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
return false;
}
return true;
}
这是我的 gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "br.com.adley.myseriesproject"
minSdkVersion 17
targetSdkVersion 23
versionCode 7
versionName "Beta 5.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'org.ini4j:ini4j:0.5.4'
compile 'jp.wasabeef:picasso-transformations:2.1.0'
compile 'com.google.firebase:firebase-core:9.0.0'
compile 'com.google.firebase:firebase-ads:9.0.0'
// http://mvnrepository.com/artifact/com.balysv/material-ripple
compile group: 'com.balysv', name: 'material-ripple', version: '1.0.2'
}
apply plugin: 'com.google.gms.google-services'
AdView 大小没有改变,但是 SMART_BANNER 仍然是新方向的全宽广告,因此 fit.I 认为重新创建 AdView这种情况下用代码比 XML.
更容易
Admob ad not resizing correctly upon screen orientation [Includes Pictures]
希望这对您有所帮助..
我遇到了一个问题,当我改变方向屏幕时,smart_banner 没有刷新。
我搜索了一下 smart_banner 应该会自动刷新横幅的大小,但他们没有这样做,我不明白为什么。
编辑:我如何解决问题
我只是通过 onConfigurationChanged() 上的代码删除、重新绑定和重新添加我的 Adview:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Remove the ad keeping the attributes
AdView ad = (AdView) findViewById(R.id.ad_view_home);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) (ad != null ? ad.getLayoutParams() : null);
RelativeLayout parentLayout = (RelativeLayout) (ad != null ? ad.getParent() : null);
if (parentLayout != null) {
parentLayout.removeView(ad);
}
// Re-initialise the ad
mAdView.destroy();
mAdView = new AdView(this);
mAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
mAdView.setAdUnitId(getString(R.string.banner_ad_unit_id_home));
mAdView.setId(R.id.ad_view_home);
mAdView.setLayoutParams(lp);
if (parentLayout != null) {
parentLayout.addView(mAdView);
}
// Re-fetch add and check successful load
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.device_id_test1))
.build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Utils.setLayoutVisible(mAdView);
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Utils.setLayoutInvisible(mAdView);
}
});
}
AndroidManifest.xml:
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
我正在 XML 中设置广告视图:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="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="br.com.adley.myseriesproject.activities.HomeActivity">
<content here...>
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id_search_show"/>
</RelativeLayout>
在活动中,我这样设置:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
activateToolbarWithNavigationView(HomeActivity.this);
//Ad Config
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, getString(R.string.application_id_ad));
// Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
// values/strings.xml.
mAdView = (AdView) findViewById(R.id.ad_view_home);
// Create an ad request. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
mAdRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.device_id_test1))
.build();
// Start loading the ad in the background.
mAdView.loadAd(mAdRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Utils.setLayoutVisible(mAdView);
}
@Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
Utils.setLayoutInvisible(mAdView);
}
});
// Tabs Setup
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.home_pager);
if (tabLayout != null) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.favorites_label_fragment)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.air_today_label_fragment)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final HomePageAdapter adapter = new HomePageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
if (viewPager != null) {
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_refresh) {
return false;
}
return true;
}
这是我的 gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "br.com.adley.myseriesproject"
minSdkVersion 17
targetSdkVersion 23
versionCode 7
versionName "Beta 5.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'org.ini4j:ini4j:0.5.4'
compile 'jp.wasabeef:picasso-transformations:2.1.0'
compile 'com.google.firebase:firebase-core:9.0.0'
compile 'com.google.firebase:firebase-ads:9.0.0'
// http://mvnrepository.com/artifact/com.balysv/material-ripple
compile group: 'com.balysv', name: 'material-ripple', version: '1.0.2'
}
apply plugin: 'com.google.gms.google-services'
AdView 大小没有改变,但是 SMART_BANNER 仍然是新方向的全宽广告,因此 fit.I 认为重新创建 AdView这种情况下用代码比 XML.
更容易Admob ad not resizing correctly upon screen orientation [Includes Pictures]
希望这对您有所帮助..