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 动态设置颜色_第1张图片

更多相关文章

  1. Android Kotlin 动态申请权限
  2. Android Studio编译动态替换清单文件AndroidManifest.xml内容(找
  3. Android Textview实现颜色渐变滚动效果
  4. Android 6.0动态权限管理
  5. Android: VideoView做背景仿常见APP登录动态页面
  6. android Intent打开各种类型文件
  7. [转] Android 在程序中如何动态的修改程序图标
  8. Android TextView内容设置超链接、颜色、字体
  9. Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等

随机推荐

  1. android studio实现打电话
  2. tools:context =“activityname”布局文
  3. android 在SD卡创建文件夹
  4. Android中编码实现软件界面
  5. Android(安卓)基础
  6. android 复制、粘贴文字
  7. 「Android」使用SAF打开指定目录
  8. Android(安卓)APP--建立简单的交互界面
  9. android手势识别总结
  10. Android(安卓)显示刷新频率的实现代码