【问题标题】:Add a Admob banner bottom the page without overlapping content在页面底部添加 Admob 横幅,内容不重叠
【发布时间】:2014-05-31 11:20:47
【问题描述】:

我想在页面底部安装横幅,但它与主屏幕中的内容重叠。 在主屏幕上有很多内容,并且有滚动。

有人知道为什么吗? 谢谢。

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidsam="http://schemas.android.com/apk/res/com.jameselsey"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout            <!-- CONTENT LAYOUT HERE -->

            android:id="@+id/flContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
        <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ad_layout" <!-- AdMob LAYOUT -->
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/home_layout"
        android:layout_alignParentBottom="true"
        android:gravity="bottom"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

MainActivity.java

// Create an ad.
adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

// Add the AdView to the view hierarchy. The view
// will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);
layout.getBottom();
layout.addView(adView);

// Create an ad request. Check logcat output for the
// hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE").build();

// Start loading the ad in the background.
adView.loadAd(adRequest);

【问题讨论】:

    标签: android xml android-layout admob android-linearlayout


    【解决方案1】:

    您可以使用相对定位来解决它,例如尝试将above 属性设置为您的home_layout。-

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ad_layout"
        android:orientation="vertical" >
    

    编辑

    此外,您需要从您的ad_layout 中删除属性layout_alignBottom

    android:layout_alignBottom="@+id/home_layout"
    

    编辑 2

    这里有一个基本示例来说明您的场景。-

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:background="@color/green"
            android:layout_above="@+id/banner" />
    
        <LinearLayout
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="200dp" 
            android:orientation="vertical"
            android:background="@color/gray" />    
    </RelativeLayout>
    

    这会导致。-

    另一种方法是将您的根RelativeLayout 更改为垂直LinearLayout,也就是说,如果您坚持使用RelativeLayout,则应该删除orientation 属性。

    【讨论】:

    • 我这样做了,但我得到一个错误 - IllegalStateException:RelativeLayout 中不存在循环依赖项
    • 这行不通。如果内容少 - AdMob 高于内容,否则 - 不显示 AdMob
    • Mmh 仍然出现错误或仍然出现重叠?
    • 我明白了...也许您应该将home_layout 高度设置为match_parent。我添加了一些示例代码。
    • 我正在插入此代码。下面的空间在那里做广告,但 AdMob 不在那里显示......(但更好)
    【解决方案2】:

    我遇到了同样的问题,我已经通过这种方式解决了http://www.ahotbrew.com/how-to-put-admob-banner-on-the-bottom/

    创建一个相对布局,并将 "android:layout_alignParentBottom="true"" 属性添加到 adview,如示例中所示。给列表一个margin-bottom,它不会重叠。

    【讨论】:

    • 如果没有互联网连接,因此没有广告可以展示,我可以让现有内容使用整个屏幕吗?我有一个滚动视图,然后是广告横幅。请澄清。
    • 我还没有测试过,但是示例中有两个地方可以帮助解决这个问题。检查行:params.setMargins(0, 0, 0, adView.getHeight()+ 15);如果没有互联网连接,请尝试将其设置为 0。否则或另外尝试以编程方式修改视图“adView”的 layout_height 并将其设置为“match_parent”。
    • 感谢您的提示。我会试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多