相对布局底部视图的交替可见性

时间:2015-11-04 11:07:58

标签: android android-layout admob android-relativelayout

我有一个相对布局,并希望在布局的底部放置一个视图(AdMob视图)。这很好,就像这样:

Templates section of the Play 2.3 migration guide

现在,如果没有互联网连接,我想用占位符ImageView替换AdMob视图。我想通过将AdMob视图可见性设置为GONE并将ImageView可见性设置为VISIBLE来实现此目的。但结果看起来并不正确 - ImageView现在位于顶部红色和绿色部分,如下所示:

Play version which is used

这是布局xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_margin"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <LinearLayout
        android:id="@+id/main"
        android:layout_above="@id/ad_view"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            android:background="#AA9999"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LEFT SIDE"
                 />

            <TextView
                android:text="some long text here"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <View
            android:layout_width="@dimen/activity_margin"
            android:layout_height="match_parent" />

        <LinearLayout
            android:background="#99AA99"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RIGHT SIDE" />

            <TextView
                android:text="MORE LONG TEXT HERE"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </LinearLayout>

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:visibility="gone"
        app:adSize="BANNER"
        app:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

    <ImageView
        android:background="#000000"
        android:id="@+id/ad_placeholder"
        android:src="@drawable/ad_placeholder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:visibility="visible" />

</RelativeLayout>

我尝试将ImageView和AdMob视图放在一个线性布局中(称为底部线性布局),并使主线性布局(@ id / main)在底部线性布局上使用“layout_above”。但代码不会编译。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:orientation="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                tools:showIn="@layout/app_bar_main">

    <!--Using relative layout to get the ad align property, could not get linear layout to work properly.-->

    <LinearLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adMob_container_my"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#AA9999"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LEFT SIDE"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="some long text here"/>

        </LinearLayout>

        <View
            android:layout_width="10dp"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="#99AA99"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="RIGHT SIDE"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="MORE LONG TEXT HERE"/>

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/adMob_container_my"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal">

        <com.google.android.gms.ads.AdView
            android:id="@+id/ad_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="invisible"
            app:adSize="BANNER">
        </com.google.android.gms.ads.AdView>

        <ImageView
            android:id="@+id/ad_placeholder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="#000000"
            android:src="@drawable/ic_pango_logo_title"
            android:visibility="visible"/>

    </RelativeLayout>


</RelativeLayout>

你忘了这里的'+'号:android:layout_above="@+id/adMob_container_my"