我正在创建一个类并设置“文本样式”。我已经在attrs和styleable和style中进行了定义,并且使getter setter和set textStyle变为粗体,而文本没有变为粗体。如何设置粗体?请帮助?
<declare-styleable name="CustomTagGroup">
<attr name="descriptionTextStyle" format="flags">
<flag name="normal" value="0" />
<flag name="bold" value="1" />
<flag name="italic" value="2" />
<flag name="bolditalic" value="3" />
</attr>
<style name="CustomTagGroup">
<item name="atg_horizontalPadding">12dp</item>
<item name="atg_verticalPadding">3dp</item>
<item name="android:textStyle">bold</item>
<item name="descriptionTextStyle">bolditalic</item>
</style>
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTagGroup, defStyleAttr, R.style.CustomTagGroup);
try {
a.getType(R.styleable.CustomTagGroup_descriptionTextStyle);
int bold = Typeface.BOLD_ITALIC;
textStyle = a.getInt(R.styleable.CustomTagGroup_descriptionTextStyle, bold);
}catch(Exception e){
e.printStackTrace()
}
答案 0 :(得分:0)
这是您扩展了 TextView 的自定义类吗? 如果是,则首先获取您在xml中定义的属性taht,
<com.package.CustomTextView
......
app:text_style = "1"
/>
获取属性值后,通过以下代码添加setBold
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// set your if else condition that come from attr
// Like you get 1 then execute below line
this.setTypeface(getTypeface(),Typeface.BOLD);
}