Android 如何为3种不同的状态设置按钮样式
我正在尝试创建我自己版本的Simon says。我想为按钮创建三种状态(颜色);关闭(假),闪烁按下(真)。如果我使用Android 如何为3种不同的状态设置按钮样式,android,button,Android,Button,我正在尝试创建我自己版本的Simon says。我想为按钮创建三种状态(颜色);关闭(假),闪烁按下(真)。如果我使用mButton1.setbackgroundColor(Color.Red)我将丢失XML按钮样式。如何做到这一点而不丢失XML样式,或者是否有更好的方法来设置按钮的样式 您可以在drawable目录中创建按钮选择器xml,如下所示。它定义了当按钮处于不同状态时要使用的不同可绘制项,例如:默认、按下等 drawable/my\u button\u selector.xml <
mButton1.setbackgroundColor(Color.Red)
我将丢失XML按钮样式。如何做到这一点而不丢失XML样式,或者是否有更好的方法来设置按钮的样式 您可以在drawable
目录中创建按钮选择器
xml,如下所示。它定义了当按钮处于不同状态时要使用的不同可绘制项,例如:默认、按下等
drawable/my\u button\u selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/my_button_pressed" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/my_button_pressed" />
<!-- selected -->
<item android:state_selected="true" android:drawable="@drawable/my_button_pressed" />
<!-- default -->
<item android:drawable="@drawable/my_button_default" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/default_button_color"/>
<!-- this makes the rounded corners button -->
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/pressed_button_color"/>
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_material_light">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:drawable="@drawable/my_button_default" />
</ripple>
drawable/my_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/my_button_pressed" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/my_button_pressed" />
<!-- selected -->
<item android:state_selected="true" android:drawable="@drawable/my_button_pressed" />
<!-- default -->
<item android:drawable="@drawable/my_button_default" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/default_button_color"/>
<!-- this makes the rounded corners button -->
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/pressed_button_color"/>
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_material_light">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:drawable="@drawable/my_button_default" />
</ripple>
请注意,对于API级别21或更高级别的,可以使用默认的ripple
效果。将下面的my_button_selector.xml
放在drawable-v21
目录下:
drawable-v21/my_button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/my_button_pressed" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/my_button_pressed" />
<!-- selected -->
<item android:state_selected="true" android:drawable="@drawable/my_button_pressed" />
<!-- default -->
<item android:drawable="@drawable/my_button_default" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/default_button_color"/>
<!-- this makes the rounded corners button -->
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- define this color in your colors.xml -->
<solid android:color="@color/pressed_button_color"/>
<corners android:radius="5dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_material_light">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="5dp" />
</shape>
</item>
<item android:drawable="@drawable/my_button_default" />
</ripple>
有了这个,Android将使用v21/my_button\u selector.xml
作为API级别21或更高的
。对于低于该级别的API,它使用drawable/my_button\u selector.xml
使用选择器。