Android中selector的初步认识(一)

来源:互联网 发布:女生清唱网络歌手 编辑:程序博客网 时间:2024/06/05 15:03

最近在看代码的时候,看到很多时候,在代码中会使用selector来控制button或listview在不同状态下样式(比如在滚动图片的例子中)


今天,我就来着重学习一下在android中selector的用法


我查了一下androidAPI文档,在API文档中,对这样的写法的名称叫做state list

 

我们可以在这个地址(http://developer.android.com/intl/zh-cn/guide/topics/resources/drawable-resource.html)查看到API中对state list的说明,本文的主要内容其实就是对API文档的翻译和解读


下面是对StateListDrawable(编译后对应的对象)的整体描述

StateListDrawable is a drawableobject defined in XML that uses a several different images to represent thesame graphic, depending on the state of the object. For example, a Button widget canexist in one of several different states (pressed, focused, or niether) and,using a state list drawable, you can provide a different background image foreach state.

You can describe thestate list in an XML file. Each graphic is represented by an <item> element insidea single <selector> element. Each <item> uses variousattributes to describe the state in which it should be used as the graphic forthe drawable.

During eachstate change, the state list is traversed top to bottom and the first item thatmatches the current state is used—the selection is not based on the "best match," but simply the first itemthat meets the minimum criteria of the state.

StateListDrawable是在XML中定义的一个可绘制(drawable)的对象,它可以根据状态的不同为同一个图形更换不同的图片。比如说,对于一个按钮控件(Button)可以有几种不同的状态(pressedfocused或者其它),你可以使用state list为其中的每一个状态来设置按钮的背景图片。

你可以在XML文件中申明state list。在Selector标签中每一对item标签对应一个图形。每个item标签中都可以设置一个属性值去表示当前状态下图形的样子。

每当状态发生改变时,系统会在state list中从上到下遍历寻找与之相匹配的状态。注意,这里在遍历时只会取去当前状态相匹配的第一个Item,而不是最匹配的那一个。


XML文件的位置:

res/drawable/filename.xml

 

编译后的数据类型:

该文件将会编译为一个StateListDrawable对象

 

如何引用该文件:

In Java: R.drawable.filename

In XML: @[package:]drawable/filename



接下来,我们看一下,在配置XML文件时,需要用到的属性:

<selector xmlns:android="http://schemas.android.com/apk/res/android"    android:constantSize=["true" | "false"]    android:dither=["true" | "false"]    android:variablePadding=["true" | "false"] >    <item        android:drawable="@[package:]drawable/drawable_resource"        android:state_pressed=["true" | "false"]        android:state_focused=["true" | "false"]        android:state_hovered=["true" | "false"]        android:state_selected=["true" | "false"]        android:state_checkable=["true" | "false"]        android:state_checked=["true" | "false"]        android:state_enabled=["true" | "false"]        android:state_activated=["true" | "false"]        android:state_window_focused=["true" | "false"] /></selector>

这是在API文档中列举出来的,在selector中可能会用到的标签及属性
下面,让我们来一一解读一下这些标签和属性:


selector标签,这个标签是必要的标签,而且应当作为XML文件的根节点,在selector可以有一或多个item子标签
在selector标签中,我们可以配置以下几种属性:
           xmlns:android="http://schemas.android.com/apk/res/android"    这个是android的命名空间,默认这么写就可以

            android:constantSize     这个是布尔型,false表示各个状态的大小(size)各自不同,true表中所有的状态大小相同(以最大的为准)。默认为false

       android:dither 布尔型。true表示,如果一个屏幕中位图有这不同的像素配置,启用位图的抖动。false表示不启用位图的抖动。默认为true度娘:dither,抖动,是一种故意造成的噪音用以随机化量化误差,阻止大幅度拉升图像时导致的像banding(色带)这样的问题。

       android:variablePadding 布尔型。选择true时,drawable的内边距会根据状态的变化而变化,设置为true时,你必须为不同的状态配置layout,但是通常不建议这么做。选择false时,内边距保持一致,所有状态中最大的内边距。默认为false


接下来是Item标签
item标签,通过一些属性的配置,定义drawable的特定状态。该标签必须作为selector的子标签。
在item标签中,我们可以配置一下几种属性

android:drawable

这个属性是必须的,为当前控件指定资源。

android:state_pressed

布尔值。true指当用户点击或者触摸该控件的状态。默认为false

android:state_focused

布尔值。ture指当前控件获得焦点时的状态。默认为false

android:state_hovered

布尔值。true表示光标移动到当前控件上的状态。默认为false

android:state_selected

布尔值。true表示被选择的状态,例如在一个下拉列表中用方向键下选择其中一个选项。

这个和focus的区别,selectedfocus不充分的情况。比如一个listview获得焦点(focus),而用方向键选择了其中的一个itemselected

android:state_checkable

布尔值。ture表示可以被勾选的状态。这个仅在当控件具有被勾选和不被勾选的状态间转换时才起作用。 

android:state_checked

布尔值。true表示当前控件处于被勾选(check的状态) 

android:state_enabled

布尔值。true表示当前控件出于可用的状态。比如可以被点击 

android:state_activated

布尔值。true表示当前控件被激活的状态。

android:state_window_focused

布尔值。true表示当前控件出于最前端时,应用窗口获得焦点的状态。


注意:安卓程序在读取这个文件时,只会读取符合当前控件状态的第一个Item的内容。如果在selector下的第一个item中没有标注以上任何一个状态,那么它表示使用任何状态,将会在空间每次状态变化时只读取这个item的内容。所以,这样的默认的配置,一般都会置于最下面的item中。


由于水平有限,翻译的质量不是很高,有什么不准确的地方希望大家指正。而且我觉得想要更深入的理解还是需要在实际的代码中,所以下一篇中我会使用一个例子来测试一下各种属性和状态有什么不同,如果在写代码中发现什么问题,我也会尽量补充到这篇博客中的。

1 0
原创粉丝点击