[英]Android layout_below in RelativeLayout
I came across a strange issue, below is my layout: 我遇到一个奇怪的问题,下面是我的布局:
<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
is not below ImageView
as expected, it's below the original position of ImageView
. TextView
不低于ImageView
预期,这是低于原来的位置ImageView
。
If i set layout_height
of RelativeLayout
to 200dp
or match_parent
, it works fine. 如果我将RelativeLayout
layout_height
设置为200dp
或match_parent
,它将正常工作。
How do i solve this? 我该如何解决?
Simple solution for you is to use android:layout_height="match_parent"
in your root layout. 一个简单的解决方案是在根目录布局中使用android:layout_height="match_parent"
。
If you are using wrap_content
for your root layout, android:layout_centerInParent
and android:layout_below
will not work properly. 如果您在根布局中使用wrap_content
,则android:layout_centerInParent
和android:layout_below
将无法正常工作。
Either use height as match_parent
or static. 使用height作为match_parent
或static。
choice 1 : 选择1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
choice 2 : 选择2:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
choice 3: 选择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>
Now its upto you to use which solution. 现在由您决定使用哪种解决方案。
If you want your Relative Layout
to Wrap Content
and Your Second Image view
Apply with layout_centerInParent
then you need to give marginTop
to Text View
. 如果您希望Relative Layout
Wrap Content
并且第二个Image view
使用layout_centerInParent
进行应用,则需要为Text View
提供marginTop
。
Try with below code. 尝试下面的代码。
<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>
Here is Screen Shot. 这是屏幕截图。
Thank for everyone answered. 谢谢大家的回答。 I have got a solution at last. 我终于找到了解决方案。 It is not a very good solution, but it works fine. 这不是一个很好的解决方案,但是效果很好。
<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>
The top answer is actually not necessarily true. 最高答案实际上不一定是正确的。 You do not need to give the element a margin top for it to be below the image view. 您无需为该元素提供一个顶部空白,使其位于图像视图下方。 In my situation, I have a RelativeLayout with an ImageView and a ListView. 在我的情况下,我有一个带有ImageView和ListView的RelativeLayout。 I wanted ImageView to be above ListView but the ListView to cover the area below the ImageView. 我希望ImageView在ListView上方,但ListView覆盖ImageView下面的区域。 And I achieved it by setting layout_alignParentTop
to the ImageView and adding layout_below
to the ListView. 我通过将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.