1

有一段时间我一直在为此撞墙。试图让 AdMob AdView 显示在 ScrollView 上方,并说 ScrollView 填满了屏幕的其余部分。到目前为止,我所能做的就是让 AdView 显示并正确加载测试广告,但是屏幕的其余部分只是黑色的。我不知道 ScrollView 到底去了哪里。

我尝试了十几种不同的解决方案,包括以编程方式而不是通过 main.xml 加载它,并将 ScrollView 的高度更改为 wrap_content,但仍然遇到同样的问题。还尝试根据另一个线程的建议将 ScrollView 的高度设置为 0px,将权重设置为 1,但这也不起作用。我猜这是一个简单的答案,但我现在很困惑。

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/mainLayout"
   >
<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     ads:adUnitId="[my ad id]"
                     ads:adSize="BANNER"
                     />
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:drawSelectorOnTop="false"
    android:background="[background drawable]">
[snip all the crap within the scrollview which is irrelevant]
</ScrollView>
</LinearLayout>

然后在我的主 .java 文件的 oncreate 调用中:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //programattically create adview
    //AdView adView = new AdView(this, AdSize.BANNER, "[my ad id]");
    //find main layout and add adview to it
    //LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
    //layout.addView(adView);

    //xml adview
    AdView adView = (AdView)this.findViewById(R.id.adView);

    //set up ad request with test devices/emulator
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    adView.loadAd(request);

有人有什么建议吗?

4

3 回答 3

1

您的初始值LinearLayout应指定android:orientation="vertical",然后改为设置AdView高度wrap_content(或 50dp - 这是我在我的一个应用程序中所拥有的)。这应该足够了。TheScrollView是正确的,你有它,使用wrap_contentand android:layout_weight="1"

于 2011-06-22T07:26:50.743 回答
0

你做了AdView高度fill_parent。取而代之的是它wrap_content的高度。ScrollViewfill_parent

于 2011-06-21T06:17:17.113 回答
0

好的,找到了一种方法来完成这项工作。有一段时间在那里有点抓狂。

我发现有效的方法是切换到RelativeLayout,以编程方式加载广告并放入一个ViewStub,当广告加载(使用AdListener)时,它会膨胀一个与广告相同高度的视图。我怀疑有一种更优雅的方法可以做到这一点,但这绝对适合我。

主.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ViewStub
    android:id="@+id/mainPlaceholder"
    android:inflatedId="@+id/mainAdInflated"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout="@layout/ad_mob_layout"
    />                         
<ScrollView 
    android:layout_below="@+id/mainAdInflated"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="[background drawable]"
    >
[snip irrelevant views]
</ScrollView>
</RelativeLayout>

ad_mob_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<View
    android:layout_width="fill_parent"
    android:layout_height="50dip"            
    />
</LinearLayout>

[主要].java:

public class [main] extends Activity implements AdListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //programattically create adview
    AdView adView = new AdView(this, AdSize.BANNER, "a14e0010a66c749");
    //find main layout and add adview to it
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
    layout.addView(adView);      

    //set up ad request with test devices/emulator
    AdRequest request = new AdRequest();
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    adView.loadAd(request);
    adView.setAdListener(this);
}

@Override
public void onReceiveAd(Ad arg0) {
    @SuppressWarnings("unused")
    View stubToInflate = ((ViewStub)this.findViewById(R.id.mainPlaceholder)).inflate();
}

如果其他人遇到此问题,希望这会有所帮助!

于 2011-06-22T05:16:07.213 回答