[英]Android layout_below in RelativeLayout
我遇到一個奇怪的問題,下面是我的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:src="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="text"
android:layout_below="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
TextView
不低於ImageView
預期,這是低於原來的位置ImageView
。
如果我將RelativeLayout
layout_height
設置為200dp
或match_parent
,它將正常工作。
我該如何解決?
一個簡單的解決方案是在根目錄布局中使用android:layout_height="match_parent"
。
如果您在根布局中使用wrap_content
,則android:layout_centerInParent
和android:layout_below
將無法正常工作。
使用height作為match_parent
或static。
選擇1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
選擇2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
選擇3:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text" />
</LinearLayout>
現在由您決定使用哪種解決方案。
如果您希望Relative Layout
Wrap Content
並且第二個Image view
使用layout_centerInParent
進行應用,則需要為Text View
提供marginTop
。
嘗試下面的代碼。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="?attr/colorPrimary" />
<ImageView
android:id="@+id/imageTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageTest"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="text"
android:textSize="22sp" />
</RelativeLayout>
這是屏幕截圖。
謝謝大家的回答。 我終於找到了解決方案。 這不是一個很好的解決方案,但是效果很好。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:src="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<RelativeLayout
android:layout_height="warp_content"
android:layout_width="match_parent"
android:layout_alignTop="@+id/background"
android:layout_alignBottom="@+id/background">
<ImageView
android:id="@+id/image"
android:src="@mipmap/ic_launcher"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="text"
android:layout_below="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
最高答案實際上不一定是正確的。 您無需為該元素提供一個頂部空白,使其位於圖像視圖下方。 在我的情況下,我有一個帶有ImageView和ListView的RelativeLayout。 我希望ImageView在ListView上方,但ListView覆蓋ImageView下面的區域。 我通過將layout_alignParentTop
設置為ImageView並將layout_below
添加到ListView來實現。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="io.menuloop.menuloopandroid.MainActivity">
<ImageView android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yourStory" android:src="@drawable/ic_face_black_24dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/yourStory"
android:layout_centerHorizontal="true" android:id="@+id/listView" />
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.