AdMob AdView 显示,但布局的其余部分消失了 (Android)

发布于 2024-11-16 12:47:58 字数 1989 浏览 8 评论 0原文

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

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

main.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);

有人有任何建议吗?

Been banging my head against the wall about this for a while. Trying to get an AdMob AdView to show up above a ScrollView and have said ScrollView fill up the remainder of the screen. So far all I've been able to do is get the AdView to show up and load the test ad properly, but then the rest of the screen is just black. I can't figure out where the hell the ScrollView's gone.

I've tried a dozen different solutions, including loading it programattically instead of via main.xml and changing the height of the ScrollView to wrap_content, but still having the same issue. Also tried setting the height of the ScrollView to 0px and the weight to 1 per another thread's suggestion, but that doesn't work either. I'm guessing this is a simple answer but I'm just stumped right now.

main.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>

and then in my main .java file's oncreate call:

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);

Anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不醒的梦 2024-11-23 12:47:58

您的初始 LinearLayout 应指定 android:orientation="vertical",然后将 AdView 高度改为 wrap_content (或 50dp - 这是我在我的一个应用程序中拥有的)。这应该足够了。 ScrollView 是正确的,因为您拥有 wrap_contentandroid:layout_weight="1"

Your initial LinearLayout should specify android:orientation="vertical", and then make the AdView height wrap_content instead (or 50dp - this is what I have in one of my apps). This should be sufficient. The ScrollView is correct as you have it, with wrap_content and android:layout_weight="1".

过潦 2024-11-23 12:47:58

您已将 AdView 高度设置为 fill_parent。将其改为 wrap_content,并将 ScrollView 的高度设置为 fill_parent

You made the AdView height fill_parent. Make it wrap_content instead and the height of the ScrollView to be fill_parent.

自我难过 2024-11-23 12:47:58

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

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

main.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>

[main].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();
}

希望这对遇到此问题的其他人有所帮助!

Okay, found a way to make this work. Got kind of maddening there for a while.

The method I found that worked was switching to a RelativeLayout, loading the ad programmatically and throwing in a ViewStub that inflates a view the same height as the ad when the ad loads (using AdListener). I suspect there's a more elegant way to do this, but this definitely works for me.

main.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>

[main].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();
}

Hope this helps if anyone else is having this problem!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文