Chrisの梳羽之礁

A look of quick intelligence and soft refinement
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

显示样式

Posted on 2011-10-19 09:45  Chrisfang6  阅读(131)  评论(0编辑  收藏  举报

 

res/drawable/selector_demo.xml

 1 <?xml version="1.0" encoding="utf-8"?> 
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/xxx"/>
4 <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/xxx"/>
5 <item android:state_pressed="true" android:drawable="@drawable/xxx"/>
6 <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/xxx"/>
7 <item android:state_enabled="true" android:drawable="@drawable/xxx"/>
8 <item android:state_focused="true" android:drawable="@drawable/xxx" android:color="#FFFFFF"/><!-- 设置文字颜色 -->
9 <item android:state_selected="true">
10 <!-- 设置渐变 -->
11 <shape>
12 <gradient android:startColor="#8600ff"/>
13 <stroke android:width="2dp" android:color="#000000"/>
14 <corners android:radius="5dp"/>
15 <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/>
16 </shape>
17 </item>
18 <item android:drawable="@drawable/xxx"/>
19 </selector>

 

使用:

  1. 在listview中配置:
    1 <ListView
    2 android:listSelector="@drawable/selector_demo
    3 />
  2. 在listview的item中添加属性: 
    1 <View
    2 android:background=“@drawable/list_item_bg"
    3 />
  3. 在Java代码中: 
    1 Drawable drawable = getResources().getDrawable(R.drawable.selector_demo); 
    2 ListView.setSelector(drawable);

    但是这样会出现列表有时候为黑的情况,需要加上:
    1 <ListView
    2 android:cacheColorHint="@android:color/transparent"
    3 />
  4. 在Java代码中:
    1 ImageView.setImageResource(R.drawable.selector_demo);