MaterialButtonToggleGroup的子项宽度相同

10

我想在 MaterialButtonToggleGroup 中使用 2 个 MaterialButton,并希望它们具有相同的宽度。如果使用 match_parent 属性,则只会显示一个按钮,使用 wrap_content 属性则会使它们居中显示。

以下是当前的输出效果:

MaterialButtonToggleGroup

下面是我的代码:

<com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggle_button_group"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center"
        app:checkedButton="@id/btn_login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_login"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btn_register"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Register" />

    </com.google.android.material.button.MaterialButtonToggleGroup>```

Please help. Thanks
3个回答

15

我刚刚自己遇到了一个问题,从版本1.1.0-beta01开始,MaterialButtonToggleGroup扩展了LinearLayout,以便可以像这样设置按钮的等宽(假设默认值为android:orientation="horizontal"):

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/toggleButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parrent"
            android:text="Button 1"
            style="?attr/materialButtonOutlinedStyle"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parrent"
            android:text="Button 2"
            style="?attr/materialButtonOutlinedStyle"
            />
</com.google.android.material.button.MaterialButtonToggleGroup>

1
我找不到MaterialButtonToggleGroup按钮组的自动宽度选项。您可以使用自定义值。
<com.google.android.material.button.MaterialButtonToggl
    android:layout_margin="8dp"
    android:id="@+id/ToggleGroupBrother"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:singleSelection="true"
    app:checkedButton="@+id/ToggleButton0">
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton0"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="0"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="1"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/ToggleButton2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="2"
        app:backgroundTint="@color/white"
        style="@style/MaterialButtonStyle"/>

0

您可以像这样将两个按钮的宽度设置为最大绘制宽度:

        materialButton2.post(new Runnable() {
                    @Override
                    public void run() {
                        int maxWidth = Math.max(materialButton1.getWidth(), materialButton2.getWidth());
                        materialButton1.setWidth(maxWidth);
                        materialButton2.setWidth(maxWidth);
                    }
                });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接