答案位于此处: Setting multiple different Typefaces to the TextViews in a ListView
我在列表中有20种字体,可根据用户的选择更改TextView。我想继续添加更多的自定义字体,但每次添加时手动执行此操作变得非常繁琐。在以下用例中,用户按下打开菜单(未示出)的按钮。我想通过在菜单中为每个TextView设置一个字体来显示这些按钮的各自字体:
Typeface Aclonica = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Aclonica.ttf");
TextView fontMenuAclonica = (TextView) this.view.findViewById(R.id.fontMenuAclonica);
fontMenuAclonica.setTypeface(Aclonica);
Typeface Arimo = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Arimo-Regular.ttf");
TextView fontMenuArimo = (TextView) this.view.findViewById(R.id.fontMenuArminoRegular);
fontMenuArimo.setTypeface(Arimo);
有没有比通过反复重复此代码更有效的方法?我不能只有一个代码的基本实现和字体名称,TextView名称和字体名称的值列表?
Typeface typefaceName = Typeface.createFromAsset(getActivity().getAssets(), "fonts/font.ttf");
TextView textViewName = (TextView) this.view.findViewById(R.id.resourceId);
textViewName.setTypeface(typefaceName);
更新:我认为设置ListView更有效:
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
penis
String[] fonts = new String[]{
"Aclonica",
"Amino-Regular"
};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.<String>asList(Arrays.toString(fonts)));
for( int i = 0; i <= fonts.length - 1; i++) {
planetList[i].setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/" + fonts[i] + ".ttf"));
}
这是XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:id="@+id/rowTextView"
android:textSize="20sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:typeface="sans"
android:fontFamily="sans-serif-light" />
如何遍历创建的TextView,以便我可以以编程方式设置其字体?