Android 线性布局匹配\u父对象不匹配&\x27;不要带其他孩子';宽度算进去了吗?

Android 线性布局匹配\u父对象不匹配&\x27;不要带其他孩子';宽度算进去了吗?,android,android-linearlayout,android-layout-weight,Android,Android Linearlayout,Android Layout Weight,当我有一个图像时,我想创建一个布局,文本与布局的左侧对齐。 和一个与左边对齐的芯片 我尝试过这种布局: <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="wra

当我有一个图像时,我想创建一个布局,文本与布局的左侧对齐。 和一个与左边对齐的芯片

我尝试过这种布局:

<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_父项使芯片的宽度为0dp

我读到:

视图要求的高度或宽度的特殊值。 MATCH_PARENT表示视图希望与其父视图一样大, 减去父项的填充(如果有)。在API级别8中引入

但它是否忽略了兄弟姐妹的大小

我知道我可以使用
weight
,但我仍然想了解:
是否匹配父母
忽略兄弟姐妹的宽度


匹配\u parent
属性根据父视图进行反应
LinearLayout
的子视图(宽度为'match_parent')将占据整个宽度,并且如果方向是水平的(如我们所说的宽度),则不会容纳任何进一步的子视图

对于线性布局-容纳另一个子视图以及具有
匹配父视图的子视图;您需要使用
layout\u weight

尽管您可以使用另一个
ViewGroup
-
FrameLayout
,而无需使用
layout\u 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>


父线性布局有paddingLeft和paddingRight。此外,芯片有marginStart和marginLeft giventextview没有使芯片大小为0dp,但它覆盖了所有剩余部分,因此芯片视图是隐藏的。@yuvrajsinh但为什么芯片的40dp宽度被忽略?@RahulKhurana仍然,文本
“match\u parent”
忽略芯片的40dp宽度?如果您看到xml的预览,您可以看到芯片视图显示在线性布局之外,其定义的大小已添加到芯片
android:layout\u width=“0dp”android:layout\u weight=“1”
但是芯片仍然脱离了框架
线性布局
要求所有子视图都具有
android:layout\u weight
以根据重量成比例。仅仅用android:layout\u width=“0dp”android:layout\u weight=“1”制作芯片布局是行不通的。所有其他视图的-
文本视图
图像视图
的权重和宽度也应为0dp;这将使其成比例,在这种情况下最好使用
FrameLayout
。请尝试解决方案;我已在上述答覆中提供。