简体   繁体   中英

How to change button colors on Android?

I'm a designer and about to do some light redesign of the standard components on 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

<?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

res/color

directory.

Rest of the things remain same, as you implement in selector created in drawable folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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