更改按钮的启用颜色而不更改禁用颜色
Change Button's enabled color without changing disabled color
我正在为我的 xml 布局使用元素 Button。 来自 android.widget 的按钮 class 扩展了 TextView.
此视图可以通过 java 代码标记为启用或禁用。 .setEnabled(true|false)
.
按钮xml代码
<Button
android:id="@+id/maps_list_save_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/str_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
我想要什么:
当我的按钮启用时,我想给他一个紫色。
当我的按钮被禁用时获得灰色。
我不想做的事:
创建一个新元素并包含布局,我避免这样做是因为我想保留选定的动画、提升、填充、高度等。重新创建所有内容并不明智。
我已经尝试过的:
更改背景 = 松开内部填充,使按钮变大,我想保持 material 设计“规则”
更改主题 = 我尝试通过编辑器和代码更改主题,但发生了两件事:或者我更改了更多不是按钮的内容,或者我更改了启用并禁用相同的颜色。
即使查找文档,我也没有找到如何正确使用此元素的方法。
您可以使用如下所示的选择器来实现此目的
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape
android:shape="rectangle">
<solid android:color="Your Color"></solid>
<corners android:radius="5dp"></corners>
<padding android:bottom="2dp" android:left="2dp"
android:right="2dp" android:top="2dp"></padding>
</shape>
</item>
<item android:state_enabled="false" android:drawable="@color/colorAccent"/>
</selector>
您需要为 Button
更改 android:colorAccent
值。这可以通过将主题应用于 Button
.
来实现
里面 styles.xml
引入以下变化:
<style name="PurpleTheme" parent="AppTheme">
<item name="android:colorAccent">#8654e2</item>
</style>
然后在xml文件中声明以下两个按钮,其中第一个按钮为启用状态,第二个按钮为禁用状态。
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:theme="@style/PurpleTheme" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Button 2"
android:theme="@style/PurpleTheme" />
注意,已应用按钮:
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/PurpleThemeOverlay"
然后你会得到以下输出:
我正在为我的 xml 布局使用元素 Button。 来自 android.widget 的按钮 class 扩展了 TextView.
此视图可以通过 java 代码标记为启用或禁用。 .setEnabled(true|false)
.
按钮xml代码
<Button
android:id="@+id/maps_list_save_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/str_save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
我想要什么:
我不想做的事:
创建一个新元素并包含布局,我避免这样做是因为我想保留选定的动画、提升、填充、高度等。重新创建所有内容并不明智。
我已经尝试过的:
更改背景 = 松开内部填充,使按钮变大,我想保持 material 设计“规则”
更改主题 = 我尝试通过编辑器和代码更改主题,但发生了两件事:或者我更改了更多不是按钮的内容,或者我更改了启用并禁用相同的颜色。
即使查找文档,我也没有找到如何正确使用此元素的方法。
您可以使用如下所示的选择器来实现此目的
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<shape
android:shape="rectangle">
<solid android:color="Your Color"></solid>
<corners android:radius="5dp"></corners>
<padding android:bottom="2dp" android:left="2dp"
android:right="2dp" android:top="2dp"></padding>
</shape>
</item>
<item android:state_enabled="false" android:drawable="@color/colorAccent"/>
</selector>
您需要为 Button
更改 android:colorAccent
值。这可以通过将主题应用于 Button
.
里面 styles.xml
引入以下变化:
<style name="PurpleTheme" parent="AppTheme">
<item name="android:colorAccent">#8654e2</item>
</style>
然后在xml文件中声明以下两个按钮,其中第一个按钮为启用状态,第二个按钮为禁用状态。
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:theme="@style/PurpleTheme" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Button 2"
android:theme="@style/PurpleTheme" />
注意,已应用按钮:
style="@style/Widget.AppCompat.Button.Colored"
android:theme="@style/PurpleThemeOverlay"
然后你会得到以下输出: