样式主题按钮 android (xml)

Styling theme buttons android (xml)

我在应用程序中设置按钮样式时遇到问题,我尝试过使用选择器、主题,但 none 对我有用(或有用但未达到预期效果),有什么想法应该我更改以正确设置它?

代码:

其中一个按钮(深色主题按钮(应将颜色从黑色切换为紫色(激活后))

        <Button
            android:id="@+id/dark_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:theme="@style/buttonDark"
            android:text="dark"
            app:icon="@drawable/ic_moon"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

风格:

    <style name="buttonDark" >
        <item name="android:state_active">@color/black</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:state_pressed">@color/violet</item>
    </style>

浅色模式和深色模式(以及两者中的按钮)的表示:

您可以定义自定义样式:

    <Button
        style="@style/App.Button"

与:

<style name="App.Button" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/mtrl_btn_bg_custom_selector</item>
    <item name="android:textColor">@color/mtrl_btn_text_custom_color_selector</item>
    <item name="rippleColor">@color/mtrl_btn_custom_ripple_color</item>
</style>

背景色调选择器定义背景颜色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/black" android:state_enabled="true"/>
    <item android:alpha="0.12" android:color="...."/> <!-- disabled -->
</selector>

文本颜色选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_enabled="true"/>
    <item android:alpha="0.38" android:color="...."/> <!-- disabled -->
</selector>

纹波选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:color="@color/violet500" android:state_pressed="true"/>
    <item android:color="@color/..." android:state_focused="true" android:state_hovered="true"/>
    <item android:color="@color/..." android:state_focused="true"/>
    <item  android:color="@color/..." android:state_hovered="true"/>
    <item  android:color="@color/..."/>
</selector>