为ChipGroup中的Chip添加“权重”以填充父级宽度

问题描述 投票:0回答:1

我的

Chip
里面有 2 个
ChipGroup
。我需要它们填充屏幕的整个宽度,就像我们如何在
android:layout_weight
中使用
LinearLayout
一样。

我试过这个:

<com.google.android.material.chip.ChipGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:singleSelection="true"
    app:selectionRequired="true"
    >

        <com.google.android.material.chip.Chip
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="@dimen/pill_button_height"
            android:id="@+id/chip1"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            app:shapeAppearanceOverlay="@style/Chip.LeftRounded"
            android:text="Left"
            />

        <com.google.android.material.chip.Chip
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="@dimen/pill_button_height"
            android:id="@+id/chip2"
            style="@style/Widget.MaterialComponents.Chip.Choice"
            app:shapeAppearanceOverlay="@style/Chip.RightRounded"
            android:text="Right"
            />

</com.google.android.material.chip.ChipGroup>

但是

layout_weight
不起作用,碎片会消失,因为宽度是
0dp
。 如果有人知道如何解决这个问题,而不完全丢弃
ChipGroup
并创建我自己的实现,那将非常有帮助。

谢谢。

android android-chips
1个回答
0
投票

以下是如何使两个 Chip 占据屏幕整个宽度的示例:

<com.google.android.material.chip.ChipGroup
    android:id="@+id/chip_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checkable="true"
    app:selectionRequired="true"
    app:singleLine="true"
    app:singleSelection="true">

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

        <com.google.android.material.chip.Chip
            android:id="@+id/chip_consuption"
            style="@style/Widget.MaterialComponents.Chip.Action"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:checkable="true"
            android:checked="true"
            android:text="statistics_tile_consumption_title"
            android:textAlignment="center"
            app:checkedIconVisible="false"
            app:ensureMinTouchTargetSize="false" />

        <com.google.android.material.chip.Chip
            android:id="@+id/chip_electric"
            style="@style/Widget.MaterialComponents.Chip.Action"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:checkable="true"
            android:checked="false"
            android:text="electrical_appliances"
            android:textAlignment="center"
            app:checkedIconVisible="false"
            app:ensureMinTouchTargetSize="false"/>

    </LinearLayout>

</com.google.android.material.chip.ChipGroup>
© www.soinside.com 2019 - 2024. All rights reserved.