MaterialButtonToggleGroup 子项的 MaterialButton layout_marginStart、layout_marginEnd 不工作

MaterialButtonToggleGroup child's MaterialButton layout_marginStart, layout_marginEnd not working

如果我尝试使用 layout_marginStartlayout_marginEnd 添加边距,但对UI。我不确定为什么 layout_marginStartlayout_marginEnd 不能使用 MaterialButton当我将它们添加为 MaterialButtonToggleGroup

的子项时

 <com.google.android.material.button.MaterialButtonToggleGroup
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="@dimen/ten_dp"
                            app:singleSelection="true">

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/twentY"
                                app:icon="@drawable/ic_directions_walk_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/ten_dp"

                                app:icon="@drawable/ic_directions_car_black_24dp" />

                            <com.google.android.material.button.MaterialButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"

                                android:layout_marginStart="@dimen/ten_dp"
                                app:icon="@drawable/ic_directions_bus_black_24dp" />
                        </com.google.android.material.button.MaterialButtonToggleGroup>

In order to cohesively group multiple buttons together, MaterialButtonToggleGroup overrides the start and end margins of any children added to this layout such that child buttons are placed directly adjacent to one another.

抱歉,layout_marginStartlayout_marginEnd 在这里不起作用。阅读关于 MaterialButtonToggleGroup.

的官方指南

你可以试试 ToggleButton.

                  <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="horizontal">

                        <ToggleButton
                            android:id="@+id/tJava"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textOff="JAVA"
                            android:textOn="JAVA" />

                       <ToggleButton
                            android:id="@+id/tKotlin"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textOff="KOTLIN"
                            android:textOn="KOTLIN" />

                </LinearLayout>

然后JavaclassonCreate()部分

    tJava=findViewById(R.id.tJava);
    tKotlin=findViewById(R.id.tKotlin);


    tJava.setOnCheckedChangeListener(changeChecker);
    tKotlin.setOnCheckedChangeListener(changeChecker);

然后 changeChecker 在 onCreate() 之外运行

CompoundButton.OnCheckedChangeListener changeChecker = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                if (buttonView == tJava) {

                    tKotlin.setChecked(false);

                }
                if (buttonView == tKotlin) {

                    tJava.setChecked(false);

                }

        }
    };

目前(1.2.0-beta011.3.0-alpha01)你不能这样做。
正如 的回答所指出的:

In order to cohesively group multiple buttons together, MaterialButtonToggleGroup overrides the start and end margins of any children added to this layout such that child buttons are placed directly adjacent to one another.

有一个解决方法(不是很好,但它有效):

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

   <com.google.android.material.button.MaterialButton
       style="?attr/materialButtonOutlinedStyle"
       app:strokeColor="@color/as_background"
       app:strokeWidth="2dp"
       android:textColor="@color/white"
       app:backgroundTint="@color/my_group_selector"
       ../>

MaterialButton 中使用与 activity 背景颜色相同的笔触。 strokeWidth 作品 喜欢 一个边距。
最后使用支持选中状态的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="1.00" android:color="?attr/colorPrimary" android:state_checked="true"/>
    <item android:alpha="0.30" android:color="?attr/colorPrimary" android:state_checked="false"/>
</selector>

经过几个小时的挣扎,我终于找到了解决方案。 我们可以轻松地使用 android:insetRightandroid:insetLeft 为 Material 按钮添加间距。

这是每个按钮之间 10dp (5dp + 5dp) space 的代码示例:

<com.google.android.material.button.MaterialButtonToggleGroup
  android:id="@+id/toggleGroup"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button1"
    android:insetRight="5dp"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button2"
    android:insetRight="5dp"
    android:insetLeft="5dp"
    ... />

  <com.google.android.material.button.MaterialButton
    android:id="@+id/button3"
    android:insetLeft="5dp"
    ... />

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

希望这对你也有帮助:)