关于 android:layout_below 在 RelativeLayout 中不起作用
layout_below not working in RelativeLayout
我有一个有 3 个孩子的相对布局。
- 第一个是 ImageView 封面。
- 第二个是父级的 ImageView 头像中心和
- 第三是头像下方的 TextView 应用名称和水平居中。
理论上它应该可以工作,但我的布局没有在头像下方放置文本。相对布局有什么问题?
P/S:
谢谢!
这是我的 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="https://www.codenong.com/45498122/@drawable/android_apple"/> <ImageView android:id="@+id/logo2" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:src="https://www.codenong.com/45498122/@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> |
结果:
更改父级相对布局高度以匹配父级。它会起作用的。
喜欢这个
<?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="https://www.codenong.com/45498122/@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="https://www.codenong.com/45498122/@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的错误吗?
- 检查我更新的答案。可能是RL的问题。身份证
- 这假设相对布局必须是全高,至少我的情况不是这样,所以这个答案是错误的
<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,不,他们不是。参考这个
- 我知道,在这里我认为你的意思不同。即使有或没有,它仍然应该工作。
- 如果你给予,它就会把它当作新的资源
试试这个。
<?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="https://www.codenong.com/45498122/@drawable/android_apple" /> <ImageView android:id="@+id/logo2" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:src="https://www.codenong.com/45498122/@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> |
试试下面的代码
<?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="https://www.codenong.com/45498122/@drawable/android_apple"/> <ImageView android:id="@+id/logo" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:src="https://www.codenong.com/45498122/@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> |
THE END
二维码