drawable的用法
来源:互联网 发布:杂志排版用什么软件 编辑:程序博客网 时间:2024/06/16 11:36
(转自http://blog.sina.com.cn/s/blog_4b93170a0100qhwa.html)
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片
<?xml version="1.0" encoding="utf-8" ?><selector xmlns: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文件:
1.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加属性android:background="@drawable/xxx"
2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);
ListView.setSelector(drawable);但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。
相关属性:
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"?>
<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>
最后,需要在包含 button的xml文件里添加两项。例如main.xml 文件,需要在<Button />里加两项android:focusable="true" android:background="@drawable/button_color"
0 0
- Drawable的用法
- drawable的用法
- Drawable的用法
- OSG::Drawable, Geode的用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- android的Bitmap和Drawable用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- Android Drawable 那些不为人知的高效用法
- 基本输入输出
- 触摸屏驱动分析之——ts_test.c
- 做互联网赚钱的几种模式总结。。。
- POJ 1562
- 学习jquery01
- drawable的用法
- apache kafka系列之broker内部架构
- 在VS2010中ActiveX控件注册方法,使用regsvr32命令
- arm9-rtos的中断汇编代码分析helper2416-rawos
- oracle表空间增加问题
- uva 10542 - Hyper-drive(容斥)
- 11/13位时间戳转化为标准时间
- ZOJ-1261
- BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛