禁用按钮时如何将文本颜色更改为灰色,启用时将其更改为蓝色?

时间:2018-10-03 02:21:27

标签: android user-interface

这是我的界面。按钮“保存”被禁用。因为我更改了背景,所以“禁用的按钮”与“启用的按钮”变成了相同的颜色。

enter image description here

这是我的示例XML的代码

<Button
    style="?android:attr/buttonBarButtonStyle"
    android:background="#BFBFBF"
    android:textColor="@color/colorPrimary"
    android:id="@+id/Db_New"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="NEW" />

<Button
    style="?android:attr/buttonBarButtonStyle"
    android:background="#BFBFBF"
    android:textColor="@color/colorPrimary"
    android:id="@+id/Db_Save"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:enabled="false"
    android:text=   "SAVE" />

因为我使用它们来更改背景和文本颜色。

style="?android:attr/buttonBarButtonStyle"
android:background="#BFBFBF"
android:textColor="@color/colorPrimary" 

我正在考虑如何在禁用时将“保存”文本颜色更改为灰色,并且在启用时将其变为蓝色...

1 个答案:

答案 0 :(得分:3)

要使文本颜色不同,可以使用我发布的第一件事,即颜色状态列表资源。

如果要在每种状态下更改按钮的背景色,则可以遵循此https://blog.stylingandroid.com/state-list-drawables/。不要介意第一部分,因为它只是在谈论按钮的创建。从解释按钮状态的部分开始。