选定的MaterialToggleButton颜色为纯色 [英] selected color of MaterialToggleButton to a solid color

查看:54
本文介绍了选定的MaterialToggleButton颜色为纯色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Not able to make the selected color of MaterialToggleButton to a solid color, only a transparent shade of color primary is shown.

我尝试了以下代码,输出图像如下所示.

I tried the below set of code and the out image is shown below.

样式中的按钮主题

 <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代码

    <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>

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

I require to get a solid color as shown below

任何人都可以帮助我进行整理

can anyone please help me to sort out

推荐答案

MaterialButton的背景颜色由 backgroundTint 属性定义.

The background color of the MaterialButton is defined by the backgroundTint attribute.

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

You can change it in the xml layout or you can define a custom style:

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

具有:

<style name="ButtonStyle" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/your_button_background_selector</item>
    ...
</style>

这篇关于选定的MaterialToggleButton颜色为纯色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆