Separate AdMob banner from application content with border

我们两清 提交于 2021-02-11 12:14:46

问题


I read on this Google support page that it is against policy to have ads overlap application content, and that ads should be separated from the content with a border.

Currently, below is how I'm showing a banner ad in my app. I think this results in a breach of policy, where the ad is overlapping my content (even if the portion of the screen it overlaps is blank).

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragmentDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Main content -->
        <RelativeLayout
            android:id="@+id/fragmentDrawerContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <!-- Banner ad -->
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"/>

    </FrameLayout>

    <!-- Navigation drawer -->
    <ListView android:id="@+id/fragmentLeftDrawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:textStyle="bold"
        android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>

Question: I'm not very good with xml layouts. How can I fix the layout of activity_main.xml to make it look like the recommended layout below? Would I use layout weights, or another method?

Thank you in advance. I appreciate all advice.


回答1:


You can use a LinearLayout instead of the FrameLayout, with orientation set to vertical, to make content flow one after the other, as opposed to one on top of the other.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragmentDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        orientation="vertical">

        <!-- Main content -->
        <RelativeLayout
            android:id="@+id/fragmentDrawerContainer"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="100" />

        <!-- Banner ad -->
        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"/>

    </LinearLayout>

    <!-- Navigation drawer -->
    <ListView android:id="@+id/fragmentLeftDrawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:textStyle="bold"
        android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>

Using 0dp for the layout height and a weight of 100 will tell the LinearLayout to calculate the size of the AdView first, then fill up the remaining space with the RelativeLayout.

(In this particular case, the weight could have been any non-zero value, because we didn't give any weight to the AdView.)

For more information on layout weights, you can checkout Mark Allison's tutorials on his StylingAndroid blog:

  • http://blog.stylingandroid.com/layout-weights-part-1/
  • http://blog.stylingandroid.com/layout-weights-part-2/


来源:https://stackoverflow.com/questions/32541695/separate-admob-banner-from-application-content-with-border

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!