Android的selector,背景选择器 以及.9.png
2015-06-10 19:46
316 查看
PNG,是一种非失真性压缩位图图形文件格式。PNG格式是非失真性压缩的,允许使用类似于GIF格式的调色板技术,支持真彩色图像,并具备阿尔法通道(半透明)等特性。现在有很多人使用PNG格式于互联网及其他方面上。PNG的的英文名称为Portable Network Graphics,即便携式网络图片。另有说法是名称来源于非官方的“PNG
is Not GIF”。
什么叫.9.PNG呢,这是安卓开发里面的一种特殊的图片
这种格式的图片在android 环境下具有自适应调节大小的能力。
(1)允许开发人员定义可扩展区域,当需要延伸图片以填充比图片本身更大区域时,可扩展区的内容被延展。
(2)允许开发人员定义内容显示区,用于显示文字或其他内容
首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
<?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"
4000
/>
</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:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。
=========
转自http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6014.html
is Not GIF”。
什么叫.9.PNG呢,这是安卓开发里面的一种特殊的图片
这种格式的图片在android 环境下具有自适应调节大小的能力。
(1)允许开发人员定义可扩展区域,当需要延伸图片以填充比图片本身更大区域时,可扩展区的内容被延展。
(2)允许开发人员定义内容显示区,用于显示文字或其他内容
首先android的selector是在drawable/xxx.xml中配置的,相关图片放在同目录下。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
<?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"
4000
/>
</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:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。
=========
转自http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6014.html
相关文章推荐
- Android ViewPager多页面滑动切换以及动画效果
- android多层树形结构列表学习笔记
- 关于Android端配置极光推送
- 结合源代码详解android消息模型。
- android studio 快速进行应用程序包名更换
- Android消息系统(一)
- Android 以apk包方式共享资源(动态换肤)的实现方式
- android 多线程Thread,Runnable,AsyncTask
- Android PullToRefresh (ListView GridView 下拉刷新) 使用详解
- Android 自定义RecyclerView 实现真正的Gallery效果
- android listview 异步加载图片并防止错位
- ffmpeg 移植到 android---阿冬
- Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果
- Android onActivityResult获取返回值的用法
- AndroidDevTools简介
- android的EditText字数检测和限制
- Android-onActivityResult
- Android 媒体文件管理
- android studio左边栏消失怎么办?
- 一键删除android下面无用资源