Detailed explanation of android selector

Detailed explanation of android selector

--> Change the color of the font 
<selector xmlns:android= "http://schemas.android.com/apk/res/android" > <!-- When the current window loses focus --> <item android:color= " @android:color/black" android:state_window_focused= "false" /> <!-- when unavailable --> <item android:color= "@android:color/background_light" android:state_enabled= "false" /> <! -- When pressed --> <item android:color= "@android:color/holo_blue_light" android:state_pressed= "true" /> <!-- When selected --> <item android:color="@android:color/holo_green_dark" android:state_selected="true" /> <!-- 被激活时 --> <item android:color="@android:color/holo_green_light" android:state_activated="true" /> <!-- 默认时 --> <item android:color="@android:color/white" /> </selector> 
--> Change the background of the button
 
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 当前窗口失去焦点时 --> <item android:drawable="@drawable/bg_btn_lost_window_focused" android:state_window_focused="false" /> <!-- 不可用时 --> <item android:drawable="@drawable/bg_btn_disable" android:state_enabled="false" /> <!-- 按压时 --> <item android:drawable="@drawable/bg_btn_pressed" android:state_pressed="true" />android:drawable=<item<!-- When selected -->"@drawable/bg_btn_selected" android:state_selected="true" /> <!-- 被激活时 --> <item android:drawable="@drawable/bg_btn_activated" android:state_activated="true" /> <!-- 默认时 --> <item android:drawable="@drawable/bg_btn_normal" /> </selector>

So, in the process of using, there are still a few points to pay attention to and understand:

  1. When the selector is used as a drawable resource, the item specifies the android:drawable attribute and puts it in the drawable directory;
  2. When the selector is used as a color resource, the item specifies the android:color attribute and puts it in the color directory;
  3. The color resource can also be placed in the drawable directory. When referencing, use @drawable to refer to it, but this is not recommended. It is best to separate the drawable resource and the color resource;
  4. In addition to referencing @drawable resources, the android:drawable attribute can also refer to @color color values; but android:color can only refer to @color ;
  5. Items are matched from top to bottom. If an item is matched, it will use the item instead of the best matching rule; so to set the default state, it must be written at the end. If it is written in the front, then the back All items will not work.

In addition, there are two more useful attributes under the selector tag to talk about. After adding the following two attributes, the fade effect will appear when the state changes, but it must be supported in API Level 11 and above:

  • android:enterFadeDuration When  the state changes, the fade-in time when the new state is displayed, in milliseconds
  • android:exitFadeDuration When  the state changes, the fade-out time when the old state disappears, in milliseconds
 
--> Set selector in listview
  Finally, regarding the ListItem style of the ListView, there are two ways to set it, one is to set the android:listSelector attribute in the ListView tag, and the other is to set the android:background in the layout of the ListItem . However, the results of the two settings are different. At the same time, there are some other points to pay attention to when using ListView, which are summarized as follows

  1. android:listSelector设置的ListItem默认背景是透明的,不管你在selector里怎么设置都无法改变它的背景。所以,如果想改ListItem的默认背景,只能通过第二种方式,在ListItem的布局layout里设置android:background
  2. 当触摸点击ListItem时,第一种设置方式下,state_pressedstate_focusedstate_window_focused设为true时都会触发,而第二种设置方式下,只有state_pressed会触发。
  3. 当ListItem里有Button或CheckBox之类的控件时,会抢占ListItem本身的焦点,导致ListItem本身的触摸点击事件会无效。那么,要解决此问题,有三种解决方案:

    • 将Button或CheckBox换成TextView或ImageView之类的控件
    • 设置Button或CheckBox之类的控件设置focusable属性为false
    • 设置ListItem的根布局属性android:descendantFocusability="blocksDescendants"

    第三种是最方便,也是推荐的方式,它会将ListItem根布局下的所有子控件都设置为不能获取焦点。android:descendantFocusability属性的值有三种,其中,ViewGroup是指设置该属性的View,本例中就是ListItem的根布局:

    • beforeDescendants:ViewGroup会优先其子类控件而获取到焦点
    • afterDescendants:ViewGroup只有当其子类控件不需要获取焦点时才获取焦点
    • blocksDescendants:ViewGroup会覆盖子类控件而直接获得焦点

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325870794&siteId=291194637