关于ColorStateList

ColorStateList用于加载动态设置背景或者颜色的XML.
1.加载如下的XML
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/white" />
<item android:state_pressed="false" android:color="@color/red" />
<item android:state_focused="true" android:color="@color/gray" />
<item android:state_focused="false" android:color="@color/d_blue" />
</selector>

2.也可以直接通过代码实现以上XML的效果.如下
使用构造器[b]public ColorStateList(int[][] states, int[] colors) [/b]
具体例子:
private ColorStateList createSelector() {
int statePressed = android.R.attr.state_pressed;
int stateFocesed = android.R.attr.state_focused;
int[][] state = {{statePressed},{-statePressed},{stateFocesed},{-stateFocesed}};
int color1 = getResources().getColor(R.color.white);
int color2 = getResources().getColor(R.color.red);
int color3 = getResources().getColor(R.color.gray);
int color4 = getResources().getColor(R.color.d_blue);
int[] color = {color1, color2, color3, color4};
ColorStateList colorStateList = new ColorStateList(state,color);
return colorStateList;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值