一 使用XML定义背景
属性 | 备注 | |
shape | 矩形(rectangle)、椭圆形(oval)、线性形状 (line)、环形(ring) | shape=ring innerRadius 尺寸,内环的半径 innerRadiusRatio 浮点型,以环的宽度比率来表示内环的半径 thickness 尺寸,环的厚度 thicknessRatio 浮点型,以环的宽度比率来表示环的厚度 |
solid | color | 内部填充 |
gradient | startColor endColor centerColor angle type gradientRadius centerX centerY | 渐变色 angle 渐变角度(PS:当angle=0时,渐变色是从左向右。 然后逆时针方向转,当angle=90时为从下往上。angle必须为45的整数倍) type 渐变类型 linear 线性渐变默认 radial 以开始色为中心放射 sweep gradientRadius 整型 渐变色半径.当 android:type="radial" 时才使用 centerX 渐变中心X点坐标的相对位置 centerY 渐变中心Y点坐标的相对位置 |
stroke | width color dashWidth dashGap | dashWidth 整型 表示描边的样式是虚线的宽度, 值为0时,表示为实线 dashGap 整型 表示描边为虚线时,虚线之间的间隔 即“ - - - - ” |
corners | radius topLeftRadius topRightRadius bottomLeftRadius bottomRightRadius | 圆角 |
padding | left top right bottom | 内边距,即内容与边的距离 |
size | width height | |
二 selector (选择器)的设置
属性 | 备注 |
state_selected | 选中 |
state_focused | 获得焦点 |
state_pressed | 点击 |
state_enabled | 设置是否响应事件 |
focusable | android:focusable="true" android:background="@drawable/button_color" |
使用实例:
<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>
引用方法
1. background="@drawable/xxx"
2. Drawable drawable = getResources().getDrawable(R.drawable.xxx);
ListView.setSelector(drawable);
出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。