Android 来自Google Play服务的AdMob将执行不需要的自动滚动
我使用的是Google Play services版本13中的AdMob。我意识到,当我将广告放置在Android 来自Google Play服务的AdMob将执行不需要的自动滚动,android,admob,google-play-services,Android,Admob,Google Play Services,我使用的是Google Play services版本13中的AdMob。我意识到,当我将广告放置在ScrollView中时,AdMob将在成功从服务器获取广告后尝试执行不需要的自动滚动 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.AdV
ScrollView
中时,AdMob将在成功从服务器获取广告后尝试执行不需要的自动滚动
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>
package com.example.admob_bug;
导入com.google.android.gms.ads.AdRequest;
导入com.google.android.gms.ads.AdSize;
导入com.google.android.gms.ads.AdView;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.LinearLayout;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建adView。
adView=新adView(本);
咨询意见:setAdUnitId(“a151b03485063e0”);
adView.setAdSize(AdSize.BANNER);
//查找线性布局(假设已给定)
//属性android:id=“@+id/mainLayout”。
LinearLayout布局=(LinearLayout)findViewById(R.id.Advertision);
//将adView添加到其中。
layout.addView(adView);
//启动一个通用请求。
AdRequest AdRequest=新建AdRequest.Builder().build();
//使用ad请求加载adView。
adView.loadAd(adRequest);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共无效暂停(){
暂停;
super.onPause();
}
@凌驾
恢复时公开作废(){
super.onResume();
adView.resume();
}
@凌驾
公共空间{
adView.destroy();
super.ondestory();
}
私人咨询咨询;
}
...
...
完整的源代码可以从
在加载广告后,等待大约10秒钟(取决于网络质量)后,您就会意识到这个问题
是否有防止自动滚动的解决方法
在我从旧的AdMob6.4.1jar切换到GooglePlay服务的AdMob之前,这个问题并不存在
我正在使用设备Nexus S,Android 4.1.2进行测试。我没有用你的代码测试这一点,我只是假设:我认为问题在于你没有在版面中为横幅预留空间,一旦加载广告,它就会挤进其余部分 也许可以尝试在布局中添加一些最小高度以保留空间
<LinearLayout
android:id="@+id/advertisement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical" >
</LinearLayout>
或者最好直接在xml中定义admob广告视图,然后将loadAdOncreate设置为true,并将广告的linearLayout替换为以下内容:
<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"/>
更新您的活动\u main.xml
<LinearLayout
android:id="@+id/advertisement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
</LinearLayout>
它工作得很好。
使用nexus 4和samsung galaxy s进行测试。也使用emulator api 18。我在您的XML或代码中没有看到任何ScrollView。首先,ScrollView中不需要线性布局,您可以删除它。需要明确的是,您的AdView是否在加载广告之前占用空间?在加载广告之前,它不会占用任何空间。您需要在ScrollView中使用LinearLayout。请参考
ScrollView是一个框架布局,这意味着您应该在其中放置一个子元素,其中包含要滚动的全部内容;此子对象本身可能是具有复杂对象层次结构的布局管理器。通常使用的子项是垂直方向的线性布局,表示用户可以滚动的顶级项目的垂直数组。
Ack,my bad。奇怪的是,在加载广告之前,您的AdView没有占用任何空间。它通常会这样做。为什么不将AdView移到ScrollView之外呢?事实上,我很难为AdMob横幅预留空间,因为添加到布局中的UI组件数量是动态生成的。因此,我们可能不应该显示AdMob。只是一个想法:也许不要在onCreate方法中添加您的广告。在动态生成其余UI组件并检查广告是否在不滚动的情况下实际可见后,尝试添加并加载它?抱歉。你提出的解决方案对我不起作用。它仍然具有“自动滚动”的bug效果。或者,您可以将android:genderantfocusability=“blocksDescendants”
添加到您的PublisherAdView
,或者在codeadView.setgenderantfocusability(ViewGroup.FOCUS\u BLOCK\u substands)中执行此操作,所以,你不需要搞砸你的根视图。谈谈贬低你自己的平台。看起来广告部门和安卓部门没有相互沟通,不管怎样,他们到底在想什么来释放这种bug?