[英]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.