android学习之State List

来源:互联网 发布:指纹充电软件下载 编辑:程序博客网 时间:2024/06/05 16:38

关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。

首先android的selector是在drawable/xxx.xml中配置的。

先看一下listview中的状态:

把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。

drawable/list_item_bg.xml

<?xml version="1.0" encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

   <!-- 默认时的背景图片-->

   <item android:drawable="@drawable/pic1"/>

   <!-- 没有焦点时的背景图片-->

   <item android:state_window_focused="false"android:drawable="@drawable/pic1" />

   <!-- 非触摸模式下获得焦点并单击时的背景图片-->

   <item android:state_focused="true"android:state_pressed="true"

       android:drawable="@drawable/pic2" />

   <!-- 触摸模式下单击时的背景图片-->

   <item android:state_focused="false"android:state_pressed="true"

       android:drawable="@drawable/pic3" />

   <!--选中时的图片背景-->

   <item android:state_selected="true"android:drawable="@drawable/pic4" />

   <!--获得焦点时的图片背景-->

   <item android:state_focused="true"android:drawable="@drawable/pic5" />

</selector>

  使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg"

或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawabledrawable =getResources().getDrawable(R.drawable.list_item_bg); 

      ListView.setSelector(drawable);同样的效果。

但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"

使其透明。

其次再来看看Button的一些背景效果:

android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。

以下就是配置button中的文字效果:

drawable/button_font.xml

<?xml version="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_selected="true"android:color="#FFF" />

   <item android:state_focused="true"android:color="#FFF" />

   <item android:state_pressed="true"android:color="#FFF" />

   <item android:color="#000" />

</selector>

Button还可以实现更复杂的效果,例如渐变啊等等。

drawable/button_color.xml

 

<?xml version="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

   <itemandroid:state_pressed="true">

       <!-- 定义当button 处于pressed状态时的形态。-->

       <shape>

           <gradient android:startColor="#8600ff"/>

           <stroke android:width="2dp" android:color="#000000"/>

           <corners android:radius="5dp" />

           <padding android:left="10dp" android:top="10dp"

               android:bottom="10dp" android:right="10dp" />

       </shape>

   </item>

   <itemandroid:state_focused="true">

       <!-- 定义当button获得focus时的形态-->

       <shape>

           <gradient android:startColor="#eac100"/>

           <stroke android:width="2dp" android:color="#333333"color="#ffffff" />

           <corners android:radius="8dp" />

           <padding android:left="10dp" android:top="10dp"

               android:bottom="10dp" android:right="10dp" />

       </shape>

   </item>

</selector>

  最后,需要在包含button的xml文件里添加两项。假如是main.xml文件,我们需要在<Button />里加两项。

    android:focusable="true"

    android:backgroud="@drawable/button_color"

这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊

 

State List


A StateListDrawable is a drawable object defined in XMLthat uses a several different images to represent the same graphic,depending on the state of the object. For example, a Button widget can exist in one of several differentstates (pressed, focused, or niether) and, using a state listdrawable, you can provide a different background image for eachstate.

You can describe the state list in an XML file. Each graphic isrepresented by an <item>element inside a single<selector> element. Each<item> uses variousattributes to describe the state in which it should be used as thegraphic for the drawable.

During each state change, the state list is traversed top tobottom and the first item that matches the current state isused—the selection is not based on the "best match," butsimply the first item that meets the minimum criteria of thestate.

file location:
res/drawable/filename.xml
The filename is used as the resource ID.
compiled resource datatype:
Resource pointer to a StateListDrawable.
resource reference:
In Java: R.drawable.filename
In XML:@[package:]drawable/filename
syntax:
<?xml version="1.0" encoding="utf-8"?> 
<
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>
elements:
<selector>
Required. This must be the root element.Contains one or more<item> elements.

attributes:

xmlns:android
String. Required. Defines the XMLnamespace, which must be"http://schemas.android.com/apk/res/android".
android:constantSize
Boolean. "true" if the drawable's reported internalsize remains constant as the state changes (the size is the maximumof all of the states); "false" if the size varies based on thecurrent state. Default is false.
android:dither
Boolean. "true" to enable dithering of the bitmap ifthe bitmap does not have the same pixel configuration as the screen(for instance, an ARGB 8888 bitmap with an RGB 565 screen); "false"to disable dithering. Default is true.
android:variablePadding
Boolean. "true" if the drawable's padding shouldchange based on the current state that is selected; "false" if thepadding should stay the same (based on the maximum padding of allthe states). Enabling this feature requires that you deal withperforming layout when the state changes, which is often notsupported. Default is false.
<item>
Defines a drawable to use during certain states, as describedby its attributes. Must be a child of a<selector> element.

attributes:

android:drawable
Drawable resource. Required.Reference to a drawable resource.
android:state_pressed
Boolean. "true" if this item should be used when theobject is pressed (such as when a button is touched/clicked);"false" if this item should be used in the default, non-pressedstate.
android:state_focused
Boolean. "true" if this item should be used when theobject has input focus (such as when the user selects a textinput); "false" if this item should be used in the default,non-focused state.
android:state_hovered
Boolean. "true" if this item should be used when theobject is being hovered by a cursor; "false" if this item should beused in the default, non-hovered state. Often, this drawable may bethe same drawable used for the "focused" state.

Introduced in API level 14.

android:state_selected
Boolean. "true" if this item should be used when theobject is the current user selection when navigating with adirectional control (such as when navigating through a list with ad-pad); "false" if this item should be used when the object is notselected.

The selected state is used when focus(android:state_focused) is not sufficient (such aswhen list view has focus and an item within it is selected with ad-pad).

android:state_checkable
Boolean. "true" if this item should be used when theobject is checkable; "false" if this item should be used when theobject is not checkable. (Only useful if the object can transitionbetween a checkable and non-checkable widget.)
android:state_checked
Boolean. "true" if this item should be used when theobject is checked; "false" if it should be used when the object isun-checked.
android:state_enabled
Boolean. "true" if this item should be used when theobject is enabled (capable of receiving touch/click events);"false" if it should be used when the object is disabled.
android:state_activated
Boolean. "true" if this item should be used when theobject is activated as the persistent selection (such as to"highlight" the previously selected list item in a persistentnavigation view); "false" if it should be used when the object isnot activated.

Introduced in API level 11.

android:state_window_focused
Boolean. "true" if this item should be used when theapplication window has focus (the application is in theforeground), "false" if this item should be used when theapplication window does not have focus (for example, if thenotification shade is pulled down or a dialog appears).

Note: Remember that Androidapplies the first item in the state list that matches the currentstate of the object. So, if the first item in the list containsnone of the state attributes above, then it is applied every time,which is why your default value should always be last (asdemonstrated in the following example).

example:
XML file saved at res/drawable/button.xml:
<?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/button_pressed" /> <!-- pressed -->
 
   
<item android:state_focused="true"
 
         
android:drawable="@drawable/button_focused" /> <!-- focused -->
 
   
<item android:state_hovered="true"
 
         
android:drawable="@drawable/button_focused" /> <!-- hovered -->
 
   
<item android:drawable="@drawable/button_normal" /> <!-- default -->
 
</selector>

This layout XML applies the state list drawable to a Button:

<Button 
   
android:layout_height="wrap_content"
 
   
android:layout_width="wrap_content"
 
   
android:background="@drawable/button" />
see also:
  • StateListDrawable
0 0
原创粉丝点击