将控件高度设置为“match_parent”会使其父级更大?

时间:2015-03-15 23:13:04

标签: android xml layout

我正在尝试制作卡片视图,其中包含一些文字和图像。

This is what I want

我希望卡片视图足够大,以便文本视图可以包装到多行,所以我希望卡片可以根据文本进行缩放,我也希望图像能够填充卡片的高度,无论它是什么是

所以我将卡片视图和包含textviews height的线性布局设置为wrap_content,并将图像视图高度设置为match_parent。

然而,将图像设置为match_parent会使整个卡片更大,意味着它比文本所需的大得多吗?

有没有办法绕过这个或某种替代方法来达到同样的效果?

额外信息:

我想我知道为什么会发生这种情况,因为根据图像大小的图像查看所需大小比卡大,所以如果卡片高度为match_parent,它将达到其父级所允许的大小,直到达到原始图像的大小。因为它的父级的高度是wrap_content,所以它允许图像视图变大。除了缩小图像以外,我还没有明白如何阻止这种情况发生,但这意味着它会变得像素化?

2 个答案:

答案 0 :(得分:1)

所以最终为此工作的是将两个项目都放在相对布局中,然后将图像视图对齐到包含文本类型的线性布局的顶部和底部:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@id/text"
            android:layout_alignBottom="@id/text"/>

        <LinearLayout                
            android:id="@+id/text"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical">

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"/>

        </LinearLayout>

    </RelativeLayout>

答案 1 :(得分:0)

我不完全理解你的问题,但也许你可以做这样的事情

<LinearLayout
    android:id="@+id/layout_Parent"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <LinearLayout

        android:id="@+id/layout_ImageView"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:orientation="vertical">
    </LinearLayout>

    <LinearLayout

    android:id="@+id/layout_Text"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

这样,ImageView只会根据文本占用剩余的空间。

相关问题