问题
I were using AdMob from Google Play services version 13. I realize, when I place the advertisement within ScrollView
, AdMob will try to perform undesired auto scrolling, after it fetches the advertisement successfully from server.
package com.example.admob_bug;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adView.
adView = new AdView(this);
adView.setAdUnitId("a151b03485063e0");
adView.setAdSize(AdSize.BANNER);
// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout".
LinearLayout layout = (LinearLayout)findViewById(R.id.advertisement);
// Add the adView to it.
layout.addView(adView);
// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().build();
// Load the adView with the ad request.
adView.loadAd(adRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
private AdView adView;
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
...
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text 7"
android:layout_margin="30dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="@+id/advertisement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text 8"
android:layout_margin="30dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
The complete source code can be downloaded from https://www.dropbox.com/s/e53zjqsc5cnilz2/admob_bug.zip
You will realize this problem, after you wait for around 10 seconds (Depending on network quality), after advertisement is loaded.
Is there any workaround to prevent auto scrolling?
This problem wasn't there, before I switch from old AdMob 6.4.1 JAR, to AdMob of Google Play Service.
I'm testing using device Nexus S, Android 4.1.2.
回答1:
update your activity_main.xml
<LinearLayout
android:id="@+id/advertisement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
</LinearLayout>
it is working fine. testing with nexus 4 and samsung galaxy s.also emulator api 18.
回答2:
I didn't test this with your code, I am just assuming: I think the problem is that you didn't reserve space within your layout for the banner and once the ad is loaded it is squeezing into the rest.
Maybe try adding some min height to your layout to reserve space
<LinearLayout
android:id="@+id/advertisement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical" >
</LinearLayout>
Or maybe even better define your admob ad view directly inside the xml and then set loadAdOncreate to true and replace your linearLayout for the ad with something like this:
<com.google.ads.AdView android:id="@+id/adView"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:refreshInterval="60"
ads:adUnitId="xxxxxxxxxxxxxx"
ads:adSize="SMART_BANNER"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
来源:https://stackoverflow.com/questions/19971337/admob-from-google-play-services-will-perform-undesired-auto-scrolling