短视频平台源码,Android中 TextView设置颜色无效的问题

 短视频平台源码,Android中 TextView设置颜色无效的问题实现的相关代码

1
           tvTreble.setTextColor(R.color.white);

​由于不是用Androidstdio编译的代码,当时未发现错误,查看了源码:

 

1
    /**<br>     * Sets the text color for all the states (normal, selected,<br>     * focused) to be this color.<br>     *<br>     * @param color A color value in the form 0xAARRGGBB.<br>     * Do not pass a resource ID. To get a color value from a resource ID, call<br>     * {@link android.support.v4.content.ContextCompat#getColor(Context, int) getColor}.<br>     *<br>     * @see #setTextColor(ColorStateList)<br>     * @see #getTextColors()<br>     *<br>     * @attr ref android.R.styleable#TextView_textColor<br>     */<br>    @android.view.RemotableViewMethod<br>    public void setTextColor(@ColorInt int color) {<br>        mTextColor = ColorStateList.valueOf(color);<br>        updateTextColors();<br>    }

传了一个resource ID进去,源码告诉我们要用getColor()方法,于是

tvTreble.setTextColor(this.getResources().getColor(R.color.white)); 

这样就大功告成。

但是我在查看源码的同时意外发现:

 

1
     /**<br>     * Sets the text color.<br>     *<br>     * @see #setTextColor(int)<br>     * @see #getTextColors()<br>     * @see #setHintTextColor(ColorStateList)<br>     * @see #setLinkTextColor(ColorStateList)<br>     *<br>     * @attr ref android.R.styleable#TextView_textColor<br>     */<br>    @android.view.RemotableViewMethod<br>    public void setTextColor(ColorStateList colors) {<br>        if (colors == null) {<br>            throw new NullPointerException();<br>        }<br> <br>        mTextColor = colors;<br>        updateTextColors();<br>    }<br> <br>    /**<br>     * Gets the text colors for the different states (normal, selected, focused) of the TextView.<br>     *<br>     * @see #setTextColor(ColorStateList)<br>     * @see #setTextColor(int)<br>     *<br>     * @attr ref android.R.styleable#TextView_textColor<br>     */<br>    public final ColorStateList getTextColors() {<br>        return mTextColor;<br>    }

 

结合第一段代码的注释,如果你想要设置文字颜色随状态可变的话,第一种设置方法不管怎么样都会默认的颜色因此想要动态设置的话,我们应该

1
tvTreble.setTextColor( this.getResources().getColorStateList( R.color.text_color_pressed) );

 

以上就是  短视频平台源码,Android中 TextView设置颜色无效的问题实现的相关代码,更多内容欢迎关注之后的文章

posted @   云豹科技-苏凌霄  阅读(74)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示