android – 更改按钮的启用颜色,而不更改禁用的颜色

所以我研究了很多,有很多问题和答案谈论按钮和颜色.
但在你投票之前看到这个问题更具体,更实际,我没有找到答案.

我正在使用元素Button作为我的xml布局.来自android.widget的Button类,它扩展了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" />

我想要的是什么:

enter image description here

当我的按钮启用时,我想给他一个紫色.

enter image description here

当我的按钮被禁用时,获得灰色.

我不想做的事:

创建一个新元素并包含布局,我正在避免这个,因为我想保留选定的动画,提升,填充,提升等.再创建一切并不聪明.

我已经尝试过:

更改背景=它松开内部填充,什么使按钮更大,我想保持材料设计“规则”

enter image description here

enter image description here

更改主题=我尝试通过编辑器和代码更改主题,但发生了两件事:或者我更改了不是按钮的更多内容,或者我更改了相同颜色的启用和禁用.

即使在寻找文档,我也没有找到如何正确使用这个元素.

解决方法:

你需要的是改变android:colorAccent值,特别是Button.这可以通过将一个主题应用于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”

然后你会得到以下输出

enter image description here

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...