将AdMob横幅广告与带有边框的应用程序内容分开 [英] Separate AdMob banner from application content with border
问题描述
我在
您可以使用LinearLayout而不是FrameLayout(方向设置为Vertical)来使内容一个接一个地流动,而不是在内容顶部其他.
<?xml version ="1.0" encoding ="utf-8"?>< android.support.v4.widget.DrawerLayoutxmlns: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">< LinearLayoutandroid:layout_width ="match_parent"android:layout_height ="match_parent"orientation ="vertical"><!-主要内容->< RelativeLayoutandroid:id ="@ + id/fragmentDrawerContainer"android:layout_width ="match_parent"android:layout_height ="0dp"android:layout_weight ="100"/><!-横幅广告->< com.google.android.gms.ads.AdViewandroid: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><!-导航抽屉->< 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>
使用 0dp
表示布局高度,权重 100
会告诉LinearLayout首先计算AdView的大小,然后用RelativeLayout填充剩余的空间
(在这种情况下,权重可能是任何非零值,因为我们没有给AdView赋予任何权重.)
有关布局权重的更多信息,您可以在Mark Allison的StylingAndroid博客上查看他们的教程:
- http://blog.stylingandroid.com/layout-weights-part-1/
- http://blog.stylingandroid.com/layout-weights-part-2/
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.
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/
这篇关于将AdMob横幅广告与带有边框的应用程序内容分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!