Android 读取文本外观属性
我试图从Android 读取文本外观属性,android,android-view,android-styles,Android,Android View,Android Styles,我试图从textpearance样式中读取属性,这样我就可以将这些值应用于非平凡组件(例如TextPaint) 理想情况下,我希望收到一个引用文本外观的样式id,并读取文本颜色、字体等值 我注意到R.styleable.TextAppearance是内部的,因此无法访问,TextAppearanceAttributes类也是如此 除了这个想法还有别的选择吗 谢谢 如果使用Google材质组件,它们有一个非常有用的TextAppearance类。这是受限API,但您可以忽略以下内容: @suppr
textpearance
样式中读取属性,这样我就可以将这些值应用于非平凡组件(例如TextPaint
)
理想情况下,我希望收到一个引用文本外观的样式id,并读取文本颜色、字体等值
我注意到R.styleable.TextAppearance
是内部的,因此无法访问,TextAppearanceAttributes
类也是如此
除了这个想法还有别的选择吗
谢谢 如果使用Google材质组件,它们有一个非常有用的
TextAppearance
类。这是受限API,但您可以忽略以下内容:
@suppressint(“RestrictedApi”)
val textAppearance=textAppearance(上下文,R.style.textAppearance\u MaterialComponents\u Body1)
您需要的所有属性都可以作为TextAppearance
类的成员直接使用
如果这不是一个选项,那么您总是可以复制他们使用的代码。基本上,它涉及使用R.styleable.TextAppearance
数组及其索引从R.styleable.TextAppearance\u android\u**
中获取样式化属性(这些属性是私有的,并且是属性的一部分):
TypedArray a=context.actainStyledAttributes(id,R.styleable.TextAppearance);
textSize=a.getDimension(R.styleable.TextAppearance\u android\u textSize,0f);
textStyle=a.getInt(R.styleable.TextAppearance\u android\u textStyle,Typeface.NORMAL);
//其他属性也同样获得。。。
还有一些技巧可以在所有API级别上正确地膨胀颜色资源,获取字体也不是很简单。如果需要,可以签出代码: