相对布局底部视图的交替可见性
Alternating Visibilities of Views in Bottom of Relative Layout
我有一个相对布局,想在布局底部放置一个视图(AdMob 视图)。这工作正常,像这样:
现在,如果没有互联网连接,我想用占位符 ImageView 替换 AdMob 视图。我想通过将 AdMob 视图可见性设置为 GONE 并将 ImageView 可见性设置为 VISIBLE 来实现此目的。但是结果看起来不对 - ImageView 现在位于顶部的红色和绿色部分,如下所示:
这里是布局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) 在底部线性布局。但是代码无法编译。
有什么建议吗?
<?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"
我有一个相对布局,想在布局底部放置一个视图(AdMob 视图)。这工作正常,像这样:
现在,如果没有互联网连接,我想用占位符 ImageView 替换 AdMob 视图。我想通过将 AdMob 视图可见性设置为 GONE 并将 ImageView 可见性设置为 VISIBLE 来实现此目的。但是结果看起来不对 - ImageView 现在位于顶部的红色和绿色部分,如下所示:
这里是布局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) 在底部线性布局。但是代码无法编译。
有什么建议吗?
<?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"