Android:在扩充 xml 文件之前将字体系列设置为 textview

Android: Set font family to textview before inflate xml file

我想将 font family 设置为 Textview

但我想在加载前执行此操作xml

My code :

for(int i=0;i++;i<100000){    
    final View addNew = G.inflater.inflate(R.layout.post, null);
    TextView txtTitle = (TextView) addNew.findViewById(R.id.txtTitle);
    txtTitle.setText("" + i);
    txtTitle.setTypeface(Typeface.createFromAsset(context.getAssets(),"tahoma.ttf"));
}

这段代码耗时15-16秒

但如果我不使用 setTypeface 需要时间 2-3 秒

您可以使用绑定。看看 link

如果您不介意使用第三方库,我建议您尝试一下Calligraphy。您可以在GitHub页面上找到您需要的一切,它真的很容易使用。

你只需要在你的应用中设置好class和Activity classes,你把你的字体放在assets文件夹中,然后你就可以使用来自XML,像这样:

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>