0

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);
};
4

4 回答 4

2

由于您对每个人都在做同样的事情,TextView我倾向于创建一个方便的方法来满足您的需要,并传入R.id.xxxand 字体。例如,

private void initializeViews() {
    Typeface Helvetica = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.otf");

    final TextView Nike = initializeTextView(R.id.Nike, Helvetica);
    final TextView Puma = initializeTextView(R.id.Puma, Helvetica);
    final TextView Adidas = initializeTextView(R.id.Adidas, Helvetica);
    final TextView Reebok = initializeTextView(R.id.Reebok, Helvetica);
    final TextView Umbro = initializeTextView(R.id.Umbro, Helvetica);

}

private TextView initializeTextView(int textViewId, Typeface typeface) {
    TextView textView = (TextView)findViewById(textViewId);
    if (textView != null)
        textView.setTypeface(typeface);
    else
        Log.e(TAG, "some error message");
    return textView;
}
于 2012-11-09T02:02:11.747 回答
1

您可以存储 int ID 而不是字符串。

//String[] HelveticaTextArray = {"Nike", "Puma", "Adidas", "Reebok", "Umbro"};
int[] HelveticaTextArray = {R.id.Nike, R.id.Puma, R.id.Adidas, R.id.Reebok, R.id.Umbro}


for ( int[] HelveticaTextArray : item ) {
    final TextView HelveticaText = (TextView)findViewById(item);
    HelveticaText.setTypeface(Helvetica);
};

除非您永远不会对这些 TextView 做任何事情,否则我建议您不要这样做。findViewByID()是一个相对昂贵的调用,如果您稍后需要使用 TextView 引用,您应该保留它们而不是多次调用findViewById()

使用 HelveticaTextView 之类的子类继承 TextView 并在构造函数中设置字体,然后使用它而不是 TextView 可能是一个更好的主意,这样您就不必在 Activity 中弄乱字体了。

像这样的东西:

public class HelveticaTextView extends TextView{
    public HelveticaTextView(Context c){
        Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c);
        this.setTypeface(Helvetica);
    }
    public HelveticaTextView(Context c, AttributeSet attrs){
    Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c, attrs);
        this.setTypeface(Helvetica);
    }
    public HelveticaTextView(Context context, AttributeSet attrs, int defStyle) {
    Typeface Helvetica = Typeface.createFromAsset(c.getAssets(), "fonts/Helvetica.otf");
        super(c, attrs, defStyle);
        this.setTypeface(Helvetica);
    }
}

然后在您的 xml 布局中更改

<TextView .../>

<com.your.package.name.HelveticaTextView .../>
于 2012-11-09T01:49:59.443 回答
1
int[] HelveticaArray = {R.id.Nike, R.id.Puma, R.id.Reebok, R.id.Umbro};

for (int item : HelveticaArray) {
    ((TextView).findViewById(item)).setTypeFace(Helvetica);
}
于 2012-11-09T01:50:03.863 回答
1

大概是这样的:

TextView[] textViews = { (TextView) findViewById(R.id.Nike), (TextView) findViewById(R.id.Puma) };

for (TextView view : textViews) {
   view.setTypeface(Helvetica);
}
于 2012-11-09T01:50:39.973 回答