Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 覆盖多个视图的字体时的效率_Android_Performance_Fonts - Fatal编程技术网

Android 覆盖多个视图的字体时的效率

Android 覆盖多个视图的字体时的效率,android,performance,fonts,Android,Performance,Fonts,有两种方法可以“全局”覆盖android中使用的字体 创建扩展视图对象的类并设置类型面,即 public class TextViewRobotoRegular extends TextView { public TextViewRobotoRegular(Context context, AttributeSet attrs) { super(context, attrs); if (!isInEditMode()) { Ty

有两种方法可以“全局”覆盖android中使用的字体

创建扩展视图对象的类并设置类型面,即

public class TextViewRobotoRegular extends TextView {

    public TextViewRobotoRegular(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (!isInEditMode()) {
            Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");
            setTypeface(typeface);            
        }
    }
}
另一个是定义样式并在那里设置字体

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
    <item name="android:typeface">monospace</item>
</style>

单空间
所以我的问题是,哪一个内存效率更高


另外,XML文件中是否还有对
assets/fonts/.ttf
文件的引用?我试图让所有视图对象具有相同的字体,但如果我决定更改字体,我希望它们都引用一个点。

您正在比较两个不同的东西。您的Java代码正在加载自定义字体;您的样式只是选择一种特定的标准字体。有没有办法在xml文件中设置自定义字体?这样xml文件就可以指向项目资源中定义的特定字体?也许类似于
font/Roboto-Regular.ttf
唉,不是。不过,我的观点是,你在问内存效率,如果你比较相同的操作,这会更有意义。Java代码中的等效方法是将小部件设置为
monospace
,而不是从资产中加载
字体
(更不用说每个小部件实例加载一次)。因此我不能这样做:
字体/Roboto-Thin.ttf
,然后将此属性引用为fontfamily,如:
正确,这是不受支持的。