Android将textView设置为嵌套类的实例



我目前正在大修我的应用程序字体以使用自定义字体。

我有这样的课程:

public class TextViewRobotoRegular extends TextView {
    public TextViewRobotoRegular(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            setTypeface(Typefaces.get(context, "Roboto-BlackItalic"));           
        }
    }
}

其中 Typefaces.get是一种指向缓存字体的方法,如果没有缓存,则会创建一个新实例。

为了组织,我想知道我是否可以在一个大型类中为每种字体上几个文本视图类,类似这样

public class TextViews {
    public class RobotoRegular extends TextView {
        public TextViewRobotoRegular(Context context, AttributeSet attrs) {
            super(context, attrs);
            if (!isInEditMode()) {
                setTypeface(Typefaces.get(context, "Roboto-Thin"));           
            }
        }
    }
    public class RobotoThin extends TextView {
        public TextViewRobotoRegular(Context context, AttributeSet attrs) {
            super(context, attrs);
            if (!isInEditMode()) {
                setTypeface(Typefaces.get(context, "Roboto-Thin"));           
            }
        }
    }
}

我可以在XML布局文件中进行这些引用吗?类似:

<com.example.appname.fontPackageName.TextViews.RobotoRegular
    android:id="@+id/profileNameView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

让robotoregular和robotothin public static

和编辑XML这样:

    <view class="com.example.appname.fontPackageName.TextViews$RobotoRegular"
    android:id="@+id/profileNameView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

最新更新