Android 动态设置颜色
16lz
2021-01-23
Android 中代码定义颜色的几种方式:
1. Color.parseColor("#FFFFFF"),返回类型ColorInt.
2. Color.rgb(255,255,255),返回类型ColorInt.
3. Resources resource = (Resources) getResources();
ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.white);
在TextView与View中动态设置颜色的使用:
TextView 设置文本颜色支持的两种类型,setTextColor(@ColorInt int color)和setTextColor(ColorStateList colors).
View中设置背景色,setBackground(Drawable background)、 setBackgroundDrawable(Drawable background)、 setBackgroundColor(@ColorInt int color)和setBackgroundResource(@DrawableRes int resid),前面两种使用Drawable类型设置,Drawable可通过resource.getDrawable(resId)获取资源或者自定义Drawable,第三种支持ColorInt类型,最后一种可设置颜色值资源Id,比如setBackgroundResource(R.color.white).
设置结果:
更多相关文章
- Android Kotlin 动态申请权限
- Android Studio编译动态替换清单文件AndroidManifest.xml内容(找
- Android Textview实现颜色渐变滚动效果
- Android 6.0动态权限管理
- Android: VideoView做背景仿常见APP登录动态页面
- android Intent打开各种类型文件
- [转] Android 在程序中如何动态的修改程序图标
- Android TextView内容设置超链接、颜色、字体
- Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等