2017-08-04 117 views
0

我有3个孩子的相对布局。layout_below在RelativeLayout中不起作用

  • 1st是ImageView的封面。
  • 第二个是ImageView头像中心的父母和
  • 第三个是TextView应用程序名称下面的头像和中心水平。

在理论上它应该工作,但我的布局不会将文本放置在头像下方。 RelativeLayout有什么问题?

P/S:我已经试过android:layout_below="@+id/logo2"android:layout_below="@id/logo2",但仍然没有工作!

谢谢!

这是我的xml布局。

<?xml version="1.0" encoding="utf-8"?> 
<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="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/android_apple"/> 

    <ImageView 
     android:id="@+id/logo2" 
     android:layout_width="64dp" 
     android:layout_height="64dp" 
     android:layout_centerInParent="true" 
     android:src="@drawable/default_avatar"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logo2" 
     android:layout_centerHorizontal="true" 
     android:text="AAAAAAA" 
     android:textStyle="bold"/> 
</RelativeLayout> 

而结果:

layout_below not working

回答

2

更改父相对布局的高度来匹配父。它会工作。

像这样

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/android_apple" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
              /> 

    <ImageView 
     android:id="@+id/logo2" 
     android:layout_width="64dp" 
     android:layout_height="64dp" 
     android:layout_centerInParent="true" 
     android:src="@drawable/default_avatar"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logo2" 
     android:layout_centerHorizontal="true" 
     android:text="AAAAAAA" 
     android:textStyle="bold"/> 
</RelativeLayout> 
+0

我需要的布局高度等于ImageView封面高度 – phamxuanlu

+0

您需要将封面图像显示在整页上吗? –

+0

我的封面图片的高度未满屏幕。我只想用3个孩子的一个RelativeLayout。那是RelativeLayout的bug吗? – phamxuanlu

0

试试下面的代码

<?xml version="1.0" encoding="utf-8"?> 
<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="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/android_apple"/> 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="64dp" 
     android:layout_height="64dp" 
     android:layout_centerInParent="true" 
     android:src="@drawable/default_avatar" 
     android:layout_margin="5dp"/> 

    <RelativeLayout 
     android:id="@+id/profile_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logo" 
     android:elevation="4dp" 
     android:layout_marginTop="64dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="AAAAAAA" 
      android:textStyle="bold" 
      android:layout_margin="10dp" 
      android:layout_centerHorizontal="true"/> 
    </RelativeLayout> 
</RelativeLayout> 
0

试试这个。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginStart="10dp" 
     android:adjustViewBounds="true" 
     android:src="@drawable/android_apple" /> 

    <ImageView 
     android:id="@+id/logo2" 
     android:layout_width="64dp" 
     android:layout_height="64dp" 
     android:layout_centerInParent="true" 
     android:src="@drawable/default_avatar" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/logo2" 
     android:layout_below="@+id/logo2" 
     android:text="AAAAAAA" 
     android:textStyle="bold" /> 

</RelativeLayout> 
-1
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/logo2" 
    android:layout_centerHorizontal="true" 
    android:text="AAAAAAA" 
    android:textStyle="bold"/> 

您正在使用layout_below = “@ + ID/LOGO2”。使用layout_below =“@ id/logo2”,它会起作用。

+0

那些东西都是一样的不是吗? – Bryan

+1

@Vryin,不,他们不是。请参阅[this](https://stackoverflow.com/questions/11160954/what-is-the-difference-between-id-and-id) – Kiya

+0

我知道,在这里我认为你的意思是不同的事情。即使有或没有,它仍然应该工作。 – Bryan

相关问题