wrap_content里面的match_parent对api 18不起作用

时间:2017-08-02 09:19:20

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

我有一个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&gt; 18,这很好用: enter image description here

在API&lt; = 18上,会产生以下结果: enter image description here

有关如何修复此问题以支持旧版API的任何想法吗?

2 个答案:

答案 0 :(得分:0)

将你的布局高度设置为match_parent,将宽度设置为wrap_content 并设置 android:layout_gravity =&#34; center_vertical&#34; android:gravity = &#34; center_vertical&#34;

答案 1 :(得分:0)

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

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

您甚至可以进一步提升并移除其他 LinearLayout,但我会将其作为练习留给想要尝试的人: - )