【问题标题】:match_parent inside wrap_content doesn't work on api 18wrap_content 中的 match_parent 在 api 18 上不起作用
【发布时间】: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 > 18 上,这可以正常工作:

但是,在 API

知道如何解决此问题以支持旧 API 版本吗?

【问题讨论】:

  • 因为“match_parent inside wrap_content”根本没有意义。 (你有循环引用)
  • @Selvin 是的,但是你还能如何填充非全屏视图的高度?

标签: android xml android-layout parent-child android-relativelayout


【解决方案1】:

将您的布局高度设置为 match_parent,宽度设置为 wrap_content 并设置 android:layout_gravity="center_vertical"android:gravity="center_vertical"强>.

【讨论】:

  • 如果将第一个 LinearLayout 的高度设置为 match_parent,则元素会占据整个屏幕,这是我不想要的。父级的布局高度设置为 wrap_content。
【解决方案2】:

我还没有对此进行测试,但我注意到一些可能会导致问题的事情。尝试这些更改,看看它是如何工作的。我添加了一些 cmets 试图解释我的更改

<?xml version="1.0" encoding="utf-8"?>
<!-- Relative layout doesn't use the orientation attribute so I nixed that -->
<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">
    <!-- 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 -->
    <!-- Set the top AND bottom of this linear layout to be aligned with the first child, rather than aligning it to the top of the parent.  You also probably don't want the centerVertical attribute because that centers the LinearLayout and not it's children. --> 
    <LinearLayout
        android:id="@+id/iconWrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/childTWrapper"
        android:layout_alignTop="@+id/childTWrapper"
        android:layout_alignParentRight="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,您可以将每个 ImageView 元素与第一个子元素对齐...看起来像这样:

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

    <!-- images -->
    <ImageView
        android:id="@+id/hide"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/childTWrapper"
        android:layout_alignTop="@+id/childTWrapper"
        android:layout_toRightOf="@id/childTWrapper"
        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:layout_alignBottom="@+id/childTWrapper"
        android:layout_alignTop="@+id/childTWrapper"
        android:layout_toRightOf="@id/hide"
        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:layout_alignBottom="@+id/childTWrapper"
        android:layout_alignTop="@+id/childTWrapper"
        android:layout_toRightOf="@id/profile"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:src="@drawable/ic_browser"/>
</RelativeLayout>

您还可以更进一步,删除 other LinearLayout,但我会把它留给任何想尝试的人作为练习:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多