将 MaterialToggleButton 的选定颜色更改为纯色

2023-12-23

无法将 MaterialToggleButton 的选定颜色设置为纯色,仅显示原色的透明阴影。

我尝试了下面的代码集,输出图像如下所示。

样式中的按钮主题

 <style name="ThemeButtonColorToggle" parent="AppTheme">
        <item name="colorPrimary">@color/colorOrange</item>
        <item name="colorButtonNormal">@color/colorOrange</item>
        <item name="android:backgroundTint">@color/black</item>
        <item name="strokeColor">@color/colorOrange</item>
        <item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
        <item name="colorOnPrimary">@color/colorOrange</item>
        <item name="colorOnPrimarySurface">@color/colorOrange</item>
    </style>

XML code

    <com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:checkedButton="@id/btnAndroid"
        app:layout_constraintTop_toBottomOf="@id/tvName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:selectionRequired="true"
        app:singleSelection="true">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:theme="@style/ThemeButtonColorToggle"
            android:textColor="@android:color/white"
            android:textColorHighlight="@color/colorOrange"
            android:text="Android" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btniOS"
            android:layout_width="wrap_content"
            android:textIsSelectable="true"
            android:textColor="@android:color/white"
            android:theme="@style/ThemeButtonColorToggle"
            android:layout_height="wrap_content"
            android:text="iOS" />

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

我需要得到如下所示的纯色

谁能帮我解决一下


MaterialButton 的背景颜色由backgroundTint属性。

The 默认值 https://github.com/material-components/material-components-android/blob/1.1.0/lib/java/com/google/android/material/button/res/color/mtrl_btn_bg_color_selector.xml#L18 is:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>

您可以在 xml 布局中更改它,也可以定义自定义样式:

<com.google.android.material.button.MaterialButton
           style="@style/ButtonStyle"
           .. />

with:

<style name="ButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/your_button_background_selector</item>
    ...
</style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 MaterialToggleButton 的选定颜色更改为纯色 的相关文章

随机推荐