setHintTextColor不生效

背景

有一个定义了文本样式的style

xml 复制代码
<style name="font40">
    <item name="android:textSize">40sp</item>
    <item name="android:lineHeight">56dp</item>
</style>

代码中如下使用

scss 复制代码
setHintTextColor(Color.RED);
setTextAppearance(R.style.font40);

设置之后,会神奇的发现,hintTextColor不生效

原因

问了下豆包,豆包解释如下

查看源码可知,读取的是R.styleable.TextAppearance

找到此style

故可以看到,默认style带有textColorHint,解析之后将原本希望设置的覆盖掉了

解决办法

先setTextAppearance再setHintTextColor

交换下顺序即可

scss 复制代码
setTextAppearance(R.style.font40);
setHintTextColor(Color.RED);

自行解析style(推荐)

只解析自定义的属性,不额外解析

ini 复制代码
TypedArray array = textView.getContext().obtainStyledAttributes(style, new int[]{
        android.R.attr.textSize, android.R.attr.lineHeight});
textView.setTextSize(array.getDimensionPixelSize(0, 0));
textView.setLineHeight(array.getDimensionPixelSize(1, 0));
array.recycle();
相关推荐
针叶20 小时前
Google Play加固保护导致的崩溃
android·安全·google
执明wa1 天前
Android Studio 项目目录结构全方位详解
android·ide·android studio
__Witheart__1 天前
Android编译错误:Soong阶段因缺失res目录导致panic (Iwlan模块)
android
酿情师1 天前
逆向exe文件:CRT 初始化流程详细分析
android·软件构建·逆向·re·crt‘
问心无愧05131 天前
ctf show web入门71
android·前端·笔记
夜勤月1 天前
AQS 与 ThreadPoolExecutor 深度拆解:JDK 高并发底层设计精髓
android·java·开发语言
Yeyu1 天前
Android 卡顿诊断 SDK:从痛点出发的设计思考
android
上天_去_做颗惺星 EVE_BLUE1 天前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
我命由我123451 天前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime