Android 从stylable读取sp
我有一个自定义视图,需要从xml(可样式化)传递sp值。 我尝试的是: styleable.xml:Android 从stylable读取sp,android,Android,我有一个自定义视图,需要从xml(可样式化)传递sp值。 我尝试的是: styleable.xml: <declare-styleable name="TagView"> <attr name="tagText" format="string"/> <attr name="tagTextSize" format="dimension"/> <attr name="tagTextColor" format="color" />
<declare-styleable name="TagView">
<attr name="tagText" format="string"/>
<attr name="tagTextSize" format="dimension"/>
<attr name="tagTextColor" format="color" />
</declare-styleable>
但是它没有得到绝对的sp值(与密度相关)。。。如果我设置15sp,它就不会是15sp,而是一个非常大的尺寸,一个与密度无关的尺寸
我尝试了
typedArray.getDimension(R.styleable.TagView\u tagTextSize,0f)
,但结果相同…请尝试以下方法:
tagSize = a.getDimensionPixelSize(R.styleable.TagView_tagTextSize, 0);
if (tagSize > 0) {
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tagSize );
}
请尝试以下方法:
tagSize = a.getDimensionPixelSize(R.styleable.TagView_tagTextSize, 0);
if (tagSize > 0) {
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, tagSize );
}