将控件高度设置为 "match_parent" 会使其父项变大?
Setting a controls height to "match_parent" makes its parent bigger?
我正在尝试制作一个卡片视图,其中包含一些文本和图像。
我希望卡片视图足够大,每个文本视图都可以换行,所以我希望卡片根据文本进行缩放,我还希望图像填充卡片的高度可能是。
所以我将卡片视图和包含文本视图高度的线性布局设置为 wrap_content,并将图像视图高度设置为 match_parent。
然而,将图像设置为 match_parent 会使整张卡片变大,这意味着它比文字需要的大得多吗?
是否有任何解决方法或其他方法可以达到相同的效果?
额外信息:
我想我知道为什么会发生这种情况,这是因为基于图像尺寸的图像视图所需尺寸比卡片大,所以如果卡片高度为 match_parent,它将变得与卡片一样大它的父级允许直到它达到原始图像的大小。因为它父级的高度是 wrap_content 它让图像视图变得那么大。除了缩小图像之外,我仍然不知道如何阻止这种情况发生,但这是否意味着它会像素化?
我不完全理解你的问题,但也许你可以这样做
<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 将根据文本只占用 space 剩余空间。
所以最终的工作是让两个项目都处于相对布局中,然后将图像视图与包含文本的线性布局的顶部和底部对齐,就像这样:
<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>
我正在尝试制作一个卡片视图,其中包含一些文本和图像。
我希望卡片视图足够大,每个文本视图都可以换行,所以我希望卡片根据文本进行缩放,我还希望图像填充卡片的高度可能是。
所以我将卡片视图和包含文本视图高度的线性布局设置为 wrap_content,并将图像视图高度设置为 match_parent。
然而,将图像设置为 match_parent 会使整张卡片变大,这意味着它比文字需要的大得多吗?
是否有任何解决方法或其他方法可以达到相同的效果?
额外信息:
我想我知道为什么会发生这种情况,这是因为基于图像尺寸的图像视图所需尺寸比卡片大,所以如果卡片高度为 match_parent,它将变得与卡片一样大它的父级允许直到它达到原始图像的大小。因为它父级的高度是 wrap_content 它让图像视图变得那么大。除了缩小图像之外,我仍然不知道如何阻止这种情况发生,但这是否意味着它会像素化?
我不完全理解你的问题,但也许你可以这样做
<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 将根据文本只占用 space 剩余空间。
所以最终的工作是让两个项目都处于相对布局中,然后将图像视图与包含文本的线性布局的顶部和底部对齐,就像这样:
<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>