简体   繁体   English

如何在Android上更改按钮颜色?

[英]How to change button colors on Android?

I'm a designer and about to do some light redesign of the standard components on Android. 我是一名设计师,即将对Android上的标准组件进行一些重新设计。 Right now I'm trying to figure out how button states works. 现在,我正在尝试弄清楚按钮状态如何工作。 According to Material Design guidelines the standard buttons, Floating action button, Raised and Flat buttons, have four states: Normal, Focus, Pressed and Disabled. 根据《材料设计指南》,标准按钮,浮动动作按钮,升高按钮和展开按钮具有四种状态:正常,聚焦,按下和禁用。

  • If I only specify a color for the Normal state – will the system choose colors based on that value for the other states? 如果我仅为正常状态指定一种颜色-系统是否会基于该值为其他状态选择颜色? Is this true for other interactive elements as well such as drop downs? 其他下拉菜单也是如此吗?
  • Can I (easily) apply customized colors for the other states? 我可以(轻松地)为其他州应用自定义颜色吗?

You can customize the button state color by using selector . 您可以使用选择器来自定义按钮状态颜色。

Suppose following is button_state_color.xml file in your drawable directory 假设以下是可绘制目录中的button_state_color.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:color="hex_color"
    android:state_pressed=["true" | "false"]
    android:state_focused=["true" | "false"]
    android:state_selected=["true" | "false"]
    android:state_checkable=["true" | "false"]
    android:state_checked=["true" | "false"]
    android:state_enabled=["true" | "false"]
    android:state_window_focused=["true" | "false"] />
</selector>

Use it in your button like this 像这样在您的按钮中使用

<Button
 ...
 android:background="@drawable/button_state_color.xml"
...     
/>

No system does not take color by itself if you provide color for Normal State. 如果您为“正常状态”提供颜色,则任何系统都不会单独获得颜色。 For your second question, if you want to give shape then you can create a selector as specified by @Surace in drawable folder, but if you wish to only change color based on state then create a selector in 对于第二个问题,如果要成形,则可以在drawable文件夹中创建由@Surace指定的选择器,但是如果仅希望基于状态更改颜色,则可以在其中创建选择器。

res/color RES /颜色

directory. 目录。

Rest of the things remain same, as you implement in selector created in drawable folder. 其余内容与在可绘制文件夹中创建的选择器中实现时相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM