android background selector selector and usage instructions

Reference http://blog.sina.com.cn/s/blog_4b93170a0100qhwa.html

First, we need to create a drawable xml file in the folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_selected="false" android:drawable="@android:color/white"/>
    <item android:state_selected="true" android:drawable="@drawable/button_red"/> 
</selector>

Content selector selector


Then we add a key item in the xml file inside, using auto-complete build, we found that there are a lot item attributes can be set:

               

<-! Background image at the time of default ->     <Item Android: = drawable "@ drawable / pic1" />  

<-!  Background image when there is no focal point  ->  
  <Item Android: state_window_focused = "false"   
        drawable = "drawable @ / pic1" /: Android>   
- <!
 Gets the focus and the background under a non-touch mode when clicking picture  ->  
  <Item Android: state_focused = "to true" 
Android: state_pressed = "to true"    Android: = drawable "@ drawable / pic2" /> 

<-!  Background image when the touch mode, click ->  

<item android:state_focused="false" android:state_pressed="true"   android:drawable="@drawable/pic3" />  

<-! Picture background when selected ->  

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

<! - picture context in which to get the focus ->  

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

    

                                                

Two . Use xml file:

1. Method a: in listview configuration android: listSelector = "@ drawable /
xxx or listview of item added attribute Android :
background =" @ the drawable / XXX "

2. Method Two: . Drawable drawable = getResources () getDrawable (. R.drawable xxx );  
       ListView.setSelector (drawable);
but sometimes this is the case there will be a list of black, you need to add: Android: cacheColorHint = "@ android: color / transparent "
make it transparent. Related attributes:

android: state_selected is selected
android: state_focused
is to get the focus
android: state_pressed
clicking
android: state_enabled
is set whether to respond to events , refers to all events

These states may be provided according to the same button of the selector effect. You may be provided selector change button text state.



以下是配置button中的文字效果:
drawable/button_font.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns: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"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">        / 
<item android: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> 
<item android: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> 
最后,需要在包含 buttonxml文件里添加两项。例如main.xml 文件,需要在<Button />里加两项android
focusable="true" android:background="@drawable/button_color"





Guess you like

Origin blog.csdn.net/u011498329/article/details/46011871
Recommended