【问题标题】:layout_below not working in RelativeLayoutlayout_below 在 RelativeLayout 中不起作用
【发布时间】:2017-08-04 13:39:08
【问题描述】:

我有一个包含 3 个孩子的相对布局。

  • 第一个是 ImageView 封面。
  • 第二个是ImageView头像中心在父级和
  • 3rd 是头像下方和水平居中的 TextView 应用名称。

理论上它应该可以工作,但我的布局没有将文本放在头像下方。相对布局有什么问题?

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>

结果:

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    更改父级相对布局高度以匹配父级。它会起作用的。

    这样

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

    【讨论】:

    • 我需要布局高度等于 ImageView 覆盖高度
    • 你需要把你的封面图片显示到整页吗?
    • 我的封面图片的高度未满屏幕。而且我只想使用一个带有 3 个孩子的 RelativeLayout。是RelativeLayout的bug吗?
    • 检查我更新的答案。可能是RL的问题。 IDK
    • 这假设相对布局必须是全高,至少我的情况不是这样,所以这个答案是错误的
    【解决方案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="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>
    

    【讨论】:

      【解决方案3】:

      试试这个。

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

      【讨论】:

        【解决方案4】:
        <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",它会起作用。

        【讨论】:

        • 那些东西不是一样吗?
        • @Vryin,不,他们不是。参考this
        • 我知道,在这里我认为您的意思不同。即使有 + 或没有,它仍然应该工作。
        • 如果你给+,它会把它当作新资源
        猜你喜欢
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 2012-06-09
        相关资源
        最近更新 更多