【发布时间】:2023-04-02 14:02:01
【问题描述】:
我有一个不占据整个屏幕并拥有两个子视图的 RelativeLayout。两个孩子都是LinearLayouts,第二个孩子的高度取决于第一个孩子。
我关注了post,我对其进行了修改以生成以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#232323"
android:orientation="horizontal">
<!-- 1st child -->
<LinearLayout
android:id="@+id/childTWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/iconWrapper"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:orientation="vertical">
<TextView
android:id="@+id/lastTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingLeft="30dp"
android:paddingTop="15dp"
android:textColor="@android:color/white"
android:textStyle="bold"/>
<TextView
android:id="@+id/lastAuthor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="15dp"
android:paddingLeft="30dp"
android:textColor="@android:color/white"/>
</LinearLayout>
<!-- 2nd child; matches height of first child -->
<LinearLayout
android:id="@+id/iconWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/childTWrapper"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:paddingRight="18dp">
<!-- images -->
<ImageView
android:id="@+id/hide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:src="@drawable/ic_hide"/>
<ImageView
android:id="@+id/profile"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:src="@drawable/ic_profile"/>
<ImageView
android:id="@+id/browser"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:src="@drawable/ic_browser"/>
</LinearLayout>
</RelativeLayout>
现在,问题是我希望第二个 LinearLayout 的子级 (ImageViews) 匹配其父级的高度。这导致match_parent 视图嵌套在wrap_content 视图中。即使我将 LinearLayout 的高度更改为 match_parent,它最终仍然位于高度为 wrap_content 的 RelativeLayout 内。
但是,在 API
知道如何解决此问题以支持旧 API 版本吗?
【问题讨论】:
-
因为“match_parent inside wrap_content”根本没有意义。 (你有循环引用)
-
@Selvin 是的,但是你还能如何填充非全屏视图的高度?
标签: android xml android-layout parent-child android-relativelayout