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通过http协议POST传输方式
  2. Android(安卓)O添加Settings设置项的方法
  3. 【Android导航决】环境插件配置笔记
  4. Android之SeekBar的简单使用
  5. Top10 应用都在用的Android最新知识
  6. Android(安卓)Menu小例子
  7. Android体系结构
  8. android 5.0修改插电显示电池百分比,拔掉
  9. Android(安卓)Studio 2.3 更新小记
  10. 用android:clipChildren来实现红心变大特