微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐