【问题标题】:LinearLayout match_parent doesn't take other children's width into account?LinearLayout match_parent 不考虑其他孩子的宽度?
【发布时间】: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


【解决方案1】:

match_parent 属性根据父视图做出反应。 LinearLayout 的子视图的宽度为 `match_parent

对于 LinearLayout - 容纳另一个子视图以及具有 match_parent 的子视图;你需要使用layout_weight

尽管您可以使用另一个ViewGroup - FrameLayout 而无需使用layout_weight

以下是使用FrameLayout 作为父ViewGroup 的代码。

<FrameLayout 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="start|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"//give enough margin from your icon
       android:layout_marginLeft="@dimen/margin_between_icon_and_text"//give enough margin from your icon
       android:layout_marginRight="64dp"//give enough margin from your chip
       android:layout_gravity="start|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_marginEnd="8dp"//give enough margin from right edge of screen
       android:layout_gravity="end|center_vertical"
       android:visibility="visible" />
 </FrameLayout>

【讨论】:

  • 我已经添加到芯片android:layout_width="0dp" android:layout_weight="1",但芯片仍然出框
  • LinearLayout 要求所有子视图的 android:layout_weight 与权重成比例。仅使用 android:layout_width="0dp" android:layout_weight="1" 进行芯片布局是行不通的。所有其他视图 - TextViewImageView 的重量和宽度也应为 0dp;这将使它成比例,在这种情况下最好使用FrameLayout。请尝试解决方案;我在上面的答案中提供了。
猜你喜欢
  • 2017-05-26
  • 1970-01-01
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2021-08-13
  • 2019-07-27
  • 1970-01-01
相关资源
最近更新 更多