【发布时间】:2019-09-25 11:05:56
【问题描述】:
当我有一个图像时,我想创建一个布局,文本与布局的左侧对齐。 和一个向左对齐的芯片。
我试过这个布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/my_padding"
android:paddingBottom="@dimen/my_padding"
android:paddingLeft="@dimen/my_padding2"
android:paddingRight="@dimen/my_padding2"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/Icon"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:layout_margin="@dimen/icon_margin"
android:layout_gravity="center_horizontal|center_vertical"
android:contentDescription="@null"
android:importantForAccessibility="no"
tools:ignore="UnusedAttribute" />
<TextView
android:id="@+id/Text"
style="@style/ActionItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_between_icon_and_text"
android:layout_marginLeft="@dimen/margin_between_icon_and_text"
android:layout_gravity="center_vertical"
android:ellipsize="end"
android:gravity="start|center_vertical"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<com.my.Chip
android:id="@+id/highlight_chip"
style="@style/Widget.Chip.Suggestive"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
android:layout_gravity="end|center_vertical"
android:visibility="visible" />
</LinearLayout>
为什么textView的match_parent会使chip的宽度为0dp?
我读到了match_parent:
视图请求的高度或宽度的特殊值。 MATCH_PARENT 表示视图想要和它的父视图一样大, 减去父级的填充(如果有)。在 API 级别 8 中引入。
但它会忽略兄弟姐妹的大小吗?
我知道我可以使用weight,但仍然想了解:match_parent 是否忽略兄弟姐妹的宽度?
【问题讨论】:
-
你的父 LinearLayout 有 paddingLeft 和 paddingRight。此外,chip 有 marginStart 和 marginLeft 给定
-
textview 没有使芯片大小为 0dp,但它覆盖了所有剩余部分,因此芯片视图被隐藏。
-
@yuvrajsinh 但是为什么忽略芯片 40dp 宽度?
-
@RahulKhurana 还是,
"match_parent"的文字忽略了芯片的 40dp 宽度? -
如果你会看到你的 xml 预览,你会看到芯片视图显示在线性布局之外,其定义的大小
标签: android android-linearlayout android-layout-weight