代码设置控件selector
发布日期:2021-06-30 18:38:19
浏览次数:3
分类:技术文章
本文共 1218 字,大约阅读时间需要 4 分钟。
代码设置控件selector 作用:设置selector是为了使控件在不同的状态下显示不同的效果,如:按下时为红色背景,抬起时为白色的背景 说明:StateListDrawable相当于drawable中自定义selector的布局文件所生成的Drawable。 布局文件中的写法:java代码中的写法:
StateListDrawable stateListDrawable = new StateListDrawable();stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);//有状态的必须写在上面stateListDrawable.addState(new int[]{}, normalDrawable);//没有状态的必须写在下面/*------- normalDrawable begin --------*/GradientDrawable normalDrawable = new GradientDrawable();//得到随机颜色Random random = new Random();int alpha = 255;int red = random.nextInt(190)+30;//0-255 30-220int green = random.nextInt(190)+30;//30-220int blue = random.nextInt(190)+30;//30-220int argb = Color.argb(alpha, red, green, blue);//设置填充颜色normalDrawable.setColor(argb);//设置圆角半径normalDrawable.setCornerRadius(UIUtils.dip2Px(6));/*------- normalDrawable end --------*//*------- pressedDrawable begin --------*/GradientDrawable pressedDrawable = new GradientDrawable();pressedDrawable.setColor(Color.DKGRAY);pressedDrawable.setCornerRadius(UIUtils.dip2Px(6));/*------- pressedDrawable end --------*/view.setBackgroundDrawable(stateListDrawable);//给View设置selectorview.setClickable(true);//若view不能点击,则使其可以点击,selector才会生效
转载地址:https://linqiarui.blog.csdn.net/article/details/50764592 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月09日 03时42分21秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ACM路上的一大失误
2019-04-30
HDOJ2049 不容易系列之(4)——考新郎
2019-04-30
CodeForces 248B - Chilly Willy - 找规律
2019-04-30
POJ-2418 Hardwood Species(Trie树)(map)
2019-04-30
HDU-4300 Clairewd’s message + 4333(扩展KMP)
2019-04-30
HDU 1592 Half of and a Half(高精度)
2019-04-30
POJ-3304 Segments(计算几何)
2019-04-30
UVA-11538 Chess Queen(数学)
2019-04-30
UVA-11401 Triangle Counting(数学优化)
2019-04-30
Codeforces Round #369 (Div. 2)
2019-04-30
UVA 11426 GCD - Extreme (II)(欧拉函数)
2019-04-30
HDU-2838 Cow Sorting(树状数组)
2019-04-30
POJ-2299 Ultra-QuickSort(树状数组)(离散化)
2019-04-30
基于SSM的兼职论坛系统的设计与实现
2019-04-30
基于java的图书管理系统的设计与实现
2019-04-30
基于java的SSM框架理财管理系统的设计与实现
2019-04-30
基于java的ssm框架就业信息管理系统的设计
2019-04-30
基于java的ssm框架的旅游网站设计与实现
2019-04-30