美文网首页
Android 动态设置颜色

Android 动态设置颜色

作者: 望月Y | 来源:发表于2016-07-17 12:00 被阅读1952次

    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 动态设置颜色

          本文链接:https://www.haomeiwen.com/subject/ubfokttx.html