Android-Selector用法

在项目开发的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片。开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点击了按钮后,按钮没一点反应。于是我就再给控件添加上onTouch监听事件,按下后改变背景颜色,松手后再恢复原来颜色。但后来发现了selector这个利器,真是喜出望外,不用再添加onTouch监听事件了,用起来也方便灵活。不得不说,多和其他开发人员交流技术经验等还是很有必要的,特别是像我这样独自负责一个app开发的。
     
      android的selector要在 drawable 下配置。
      其中,selector可以设置的属性有:
                    android:state_pressed           如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

                  android:state_focused           如果是true,获得焦点时显示;如果是false没获得焦点显示默认。

                  android:state_selected          如果是true,当被选择时显示该图片;是false未被选择时显示该图片。

                  android:state_checkable         如果值为true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

                  android:state_checked           如果值为true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

                  android:state_enabled           如果值为true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
 
                  android:state_window_focused    如果值为true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片

这些属性值也可以叠加使用,比如:
                                                   android:state_window_focused="true"  android:state_pressed="true"

表明是非触摸模式下获得焦点并单击时的背景图片。

  一般来说,Button控件只需要用 android:state_pressed就可以

例子:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6
 7     <RadioGroup
 8         android:id="@+id/radioGroup1"
 9         android:layout_width="match_parent"
10         android:layout_height="60dp"
11         android:orientation="horizontal"
12         android:background="@drawable/group_buton_nomal"
13         android:gravity="center">
14
15         <RadioButton
16             android:id="@+id/radio0"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:checked="true"
20             android:text="@string/weixin"
21                style="@style/radioStyle"
22                android:drawableTop="@drawable/tab_weixin"/>
23
24     </RadioGroup>
25
26 </LinearLayout>

然后我们创建一个tab_weixin.xml的android xml文件,类型选择selector

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
3     <item android:state_checked="true"
4         android:drawable="@drawable/tabbar_mainframehl"></item>
5     <item
6         android:drawable="@drawable/tabbar_mainframe"></item>
7
8 </selector>

上面就是一个简单的selector的使用,即按钮在失去焦点做出的操作,优点类似Java的If...else

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_pressed="true" android:drawable="@drawable/bg_selected">
        </item>
    <item android:drawable="@drawable/bg_unselect">

    </item>
</selector>

在工作的时候,由于系统给出的控件不够美观,因此开发时领导常常要我更改下界面,用美工给的图片取代系统图片。开始时,我只是给按钮等设置一下背景图片,这样做虽然美观了,但界面看起来却比较死板,比如用户点击了按钮后,按钮没一点反应。于是我就再给控件添加上onTouch监听事件,按下后改变背景颜色,松手后再恢复原来颜色。但后来发现了selector这个利器,真是喜出望外,不用再添加onTouch监听事件了,用起来也方便灵活。不得不说,多和其他开发人员交流技术经验等还是很有必要的,特别是像我这样独自负责一个app开发的。
      
      android的selector要在 drawable 下配置。
      其中,selector可以设置的属性有:
                    android:state_pressed           如果是true,当被点击时显示该图片,如果是false没被按下时显示默认。

                  android:state_focused           如果是true,获得焦点时显示;如果是false没获得焦点显示默认。

                  android:state_selected          如果是true,当被选择时显示该图片;是false未被选择时显示该图片。

                  android:state_checkable         如果值为true,当CheckBox能使用时显示该图片;false,当CheckBox不能使用时显示该图片。

                  android:state_checked           如果值为true,当CheckBox选中时显示该图片;false,当CheckBox为选中时显示该图片。

                  android:state_enabled           如果值为true,当该组件能使用时显示该图片;false,当该组件不能使用时显示该图片。
 
                  android:state_window_focused    如果值为true,当此activity获得焦点在最前面时显示该图片;false,当没在最前面时显示该图片

这些属性值也可以叠加使用,比如:
                                                   android:state_window_focused="true"  android:state_pressed="true"

表明是非触摸模式下获得焦点并单击时的背景图片。

  一般来说,Button控件只需要用 android:state_pressed就可以。比如,一个Button控件的背景设置为:

  

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:state_pressed="true" android:drawable="@drawable/bg_selected">
        </item>
    <item android:drawable="@drawable/bg_unselect">

    </item>
</selector>

这表明该Button控件按下时,背景图片是bg_selected,其它情况下(没有被点击或者点击后松开等)背景图片为bg_unselect。
selector除了可以设置组件的背景颜色外,也可以设置文字的颜色。比如某个Button控件被按下后,Button上的文字颜色也发生改变。例如:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="#ffffff"></item>
    <item android:color="#000000"></item>
</selector>

即该Button控件的颜色为黑色,但按钮按下后,颜色会变成白色。松开后,颜色恢复为黑色。

以上可以在代码中这样设置:

<Button
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/btn_slector"
   android:text="abc"
   android:textColor="@drawable/text_selector"
   />

除了selector外,还有一个Shape,可以用来定制控件的图形效果,两者可以一起使用。

时间: 2024-07-28 14:20:06

Android-Selector用法的相关文章

【Android】Android背景选择器selector用法汇总

一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默认时的背景图片--> <item android:drawable="@draw

android selector(如对TextView点击样式改变)

selector 1.selector 从单词的意思来说:选择者,选择器,就是对你的目标的控制. 从API来说: A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that represents the registration. The keys are also add

Android Bitmap 用法总结

android Bitmap用法总结 Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsi

android Bitmap用法总结

Bitmap用法总结1.Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_888

Android ViewPager 用法

Android ViewPager 用法 场景:一般第一次打开应用程序时,程序会有一个提示页来给展现应用程序都有哪些功能:或者程序更新时,又更新哪些新特性,都可以使用ViewPager Demo 描述:实现三个界面的相互切换,页面颜色分别为红,绿,蓝 activity_main: 主界面只添ViewPager这一个控件 android:overScrollMode="never" 作用是当页面已经滑动到最左边或最右时,去掉阴影 <?xml version="1.0&qu

android Selector的使用

强大的selector的使用,通常我们总是在代码中进行点击按钮后在设置背景,操作起来挺麻烦的,其实有更简单的使用方法,就是selector: 布局如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_

Android HttpClient 用法以及乱码解决

一.Post提交 并可以实现多文件上传 // 创建DefaultHttpClient对象 HttpClient httpclient = new DefaultHttpClient(); // 创建一个HttpGet对象 HttpPost post = new HttpPost(realUrl); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); if (params != nu

Android Timer用法

Android考虑到线程安全问题,不允许在线程中执行UI线程,在Android中,有一个类:android.os.Handler,这个可以实现各处线程间的消息传递.先看段代码,这个实例化了一个Handler,Handler可以通过Message在多个线程通讯,这里做的是定时recLen加1,然后以一定的格式,显示到recTime上(UI线程上的操作).  1 final Handler handler = new Handler(){   2     public void handleMess

Android configChanges用法

1.    在manifest文件中使用activity的默认属性,横屏竖屏时,惠反复调用onDestory和onCreate  造成不必要的开销.Android默认如此应该是为了适配不同的xml布局文件,重新初始化资源,显示效果会好一些: 2. manifest文件中配置activity的属性: android:configChanges="orientation|screenSize" 在activity中将会回调函数: public void onConfigurationCha

Android getSystemService用法实例总结

本文实例分析了Android getSystemService用法.分享给大家供大家参考,具体如下: 1. 说明 android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监听是否有SD卡安装及移除,ClipboardService提供剪切板功能,PackageManagerService提供软件包的安装移除及查看等等,应用程序可以通过系统提供的Manager接口来访问这些Service提供的数据,以下将说明他们