android开发知识点1——按钮效果

android中控件的背景或ImageView、ImageButton的资源包括字体颜色都可以通过一个xml来定义,它是一个selector,定义了在各种状态下所显示的资源,如在正常情况下,按住状态下,选择状态下,获得或失去焦点状态下等。我们的手机是有默认的按钮效果的,但是也许我们觉得它不好看,或与我们的应用的UI不搭,这时,我们就可以通过selector,来自定义按钮的按下效果等,提高用户体验。

我的习惯是,资源一般放在drawable-hdpi,drawable-mdpi,drawable-ldpi这三个文件夹中,而对于定义selector的文件,则在res下新建一个drawable文件夹并放在里面。
该selector代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_green_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_green_normal"/>

</selector>

在以上的item的属性中,与状态相关的属性有如下12个,它们的值为true或false,定义的是在这些状态下显示的资源,并且它些属性也可以结合使用,即在一个item中定义多个state的值。
        android:state_active="" 
        android:state_checkable="" 
        android:state_checked="" 
        android:state_first="" 
        android:state_enabled="" 
        android:state_focused=" " 
        android:state_last="" 
        android:state_middle="" 
        android:state_pressed="" 
        android:state_selected="" 
        android:state_single="true" 
        android:state_window_focused=""


效果如下,看那个“记录”的按钮:

未按下效果:

按下效果:


猜你喜欢

转载自maosidiaoxian.iteye.com/blog/1708922