Android 中有禁用按钮的样式吗?

问题描述 投票:0回答:1

themes.xml 中是否有用于设置按钮禁用外观的样式/项目?我已经通过 android:colorButtonNormal 样式/项目设置了按钮的外观,但这会导致相同的样式/项目应用于启用和禁用的按钮。我本以为会有像 android:colorButtonDisabled 这样的样式/项目,但没有(至少不是那个名字)。

我正在使用 Java 8 和 Android 13 (API 33) 进行编码。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ShooterHelper" parent="Theme.AppCompat.DayNight">
        <!-- Customize your light theme here. -->

        <item name="android:statusBarColor">@color/material_dynamic_neutral30</item>
        <item name="colorPrimary">@color/material_dynamic_neutral50</item>
        <item name="android:colorBackground">@color/material_dynamic_neutral90</item>
        <item name="android:colorButtonNormal">@color/material_dynamic_neutral_variant80</item>
android android-studio material-design
1个回答
0
投票

您可以创建自己的样式并使用选择器:

res/color 创建 button_background_state.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color_is_enable" android:state_enabled="true"/>
    <item android:color="@color/color_is_disable" android:state_enabled="false"/>
</selector>

themes.xml

<style name="ButtonStyle">
        <item name="backgroundColor">@color/button_background_state</item>
</style>

在按钮布局中使用样式:

<Button
    ...
    style="@style/ButtonStyle"
    ...
    />

您可以用同样的方式定义其他颜色。

© www.soinside.com 2019 - 2024. All rights reserved.