0

的Android layout_below我遇到了一个奇怪的问题,下面是我的布局:中的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> 

enter image description here

TextView不低于ImageView如预期,这是下面的ImageView原来的位置。

如果我将layout_heightRelativeLayout设置为200dpmatch_parent,它工作正常。

我该如何解决这个问题?

+0

那么这是一个相对的布局。您需要指定每个元素相对于另一个元素的位置。要对齐行或列中的所有元素,请使用LinearLayout – Imdad

+0

为什么要添加第一个图像视图?如果要更改布局的背景色,可以使用另一种方法。 –

+0

使用match_parent代替wrap_content到根布局。 –

回答

0

简单的解决方案是在您的根布局中使用android:layout_height="match_parent"

如果您使用wrap_content作为根布局,则android:layout_centerInParentandroid:layout_below将无法​​正常工作。

使用高度为match_parent或静态。

选择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> 

现在其高达你使用哪那么lution。

0

如果你希望你的Relative LayoutWrap Content和你的第二个Image view应用与layout_centerInParent那么你需要给marginTopText View

请尝试使用以下代码。

<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> 

这里是屏幕截图。

enter image description here

+0

谢谢你的帮助。 – jfxu

+0

@xujifa请注意并接受答案,如果它对你有帮助。谢谢 :)。 –

0

感谢大家的回答。 我终于有了解决方案。这不是一个很好的解决方案,但它工作正常。

<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> 
0

最上面的答案其实不一定是正确的。您不需要给该元素留下页边以使其位于图像视图下方。在我的情况下,我有一个带有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>