ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-07-22 07:25:19  阅读:266  来源: 互联网

标签:android-button android-styles android-view android-theme 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 hereenter 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-button,android-styles,android-view,android-theme,android
来源: https://codeday.me/bug/20190722/1500324.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有