Android 如何获取TextView的样式属性

Android 如何获取TextView的样式属性,android,textview,typeface,Android,Textview,Typeface,我正在创建一个自定义TextView类MTextView。在构造函数中,我想知道textview的style attrib的值,以便根据style是否设置为粗体来设置不同的字体。但是没有getStyle()函数吗?怎么办 public class MTextView extends TextView{ public MTextView(Context context, AttributeSet attrs, int defStyle) { super(c

我正在创建一个自定义TextView类MTextView。在构造函数中,我想知道textview的style attrib的值,以便根据style是否设置为粗体来设置不同的字体。但是没有getStyle()函数吗?怎么办

    public class MTextView extends TextView{

    public MTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            if(style.equals(TypeFace.bold))  //how to get style?
                setTypeface(Typeface.createFromAsset(getContext().getAssets(),"rc.ttf"));
    }



    }

您可以从TextView的
getTypeface()
实例方法获取textStyle

int style = getTypeface().getStyle();
如果未指定textStyle(即,您希望支持普通textStyle),则
getTypeface()
可以返回null

在不为null的情况下,最好假定textStyle隐式设置为normal。

使用以下代码:

    if (attrs != null) {
        try {
            int style = attrs.getAttributeIntValue(
                "http://schemas.android.com/apk/res/android",
                "textStyle", 
                Typeface.NORMAL);

            setTypeface(Typeface.createFromAsset(
                            getContext().getAssets(), 
                            "rc.ttf"), 
                        style);
        }
        catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

这家伙显然想得到样式,但不是从默认字体,因为他从ttf文件创建它。不应标记为答案…此
if(style.equals(TypeFace.bold))
条件中的
style
是什么?申报单在哪里?