Java/Android 初学者在这里。我在我的应用程序中使用自定义字体,并且setTypeface()
为每段文本创建一个语句会影响我的代码质量。
当前代码:
Typeface Helvetica = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.otf");
final TextView Nike = (TextView)findViewById(R.id.Nike);
final TextView Puma = (TextView)findViewById(R.id.Puma);
final TextView Adidas = (TextView)findViewById(R.id.Adidas);
final TextView Reebok = (TextView)findViewById(R.id.Reebok);
final TextView Umbro = (TextView)findViewById(R.id.Umbro);
Nike.setTypeface(Helvetica);
Puma.setTypeface(Helvetica);
Adidas.setTypeface(Helvetica);
Reebok.setTypeface(Helvetica);
Umbro.setTypeface(Helvetica);
如何缩短此代码?我正在考虑一个带有数组的 foreach 循环,但我似乎无法弄清楚。这样的事情是否可能:
String[] HelveticaTextArray = {"Nike", "Puma", "Adidas", "Reebok", "Umbro"};
for ( String[] HelveticaTextArray : item ) {
final TextView HelveticaText = (TextView).findViewById(R.id.item);
HelveticaText.setTypeface(Helvetica);
};