0

我有一个带有自定义数组适配器的 ListView 设置我想更改每个单独列表项的getVIew()字体下面的方法代码确实会更改字体,但是当我向上和向下滚动时字体会发生变化

public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;

    //View row = null;

    if (v == null) {
        LayoutInflater inflater = Main.this.getLayoutInflater();
        v = inflater.inflate(R.layout.genre_list, parent, false);
        TextView item = (TextView)v.findViewById(R.id.txtListText);
        switch(position) {
        case 0:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (1).ttf");item.setTypeface(tf);
            break;
        case 1:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (2).ttf"); item.setTypeface(tf);
            break;
        case 2:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (3).ttf"); item.setTypeface(tf);
            break;
        case 3:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (4).ttf"); item.setTypeface(tf);
            break;
        case 4:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (5).ttf"); item.setTypeface(tf);
            break;
        case 5:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (6).ttf"); item.setTypeface(tf);
            break;
        case 6:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (7).ttf"); item.setTypeface(tf);
            break;
        case 7:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (8).ttf"); item.setTypeface(tf);
            break;
        case 8:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (9).ttf"); item.setTypeface(tf);
            break;
        case 9:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (10).ttf"); item.setTypeface(tf);
            break;
        default:
            tf = Typeface.createFromAsset(getAssets(),"fonts/font (10).ttf"); item.setTypeface(tf);
        }
    }

    TextView item = (TextView)v.findViewById(R.id.txtListText);
    item.setText(Genres[position]);     

    // Declare and define the TextView, "icon." This is where
    // the icon in each row will appear.

    return v;
}
4

1 回答 1

0
String[] typefaceArr = new String[] {"fonts/font (1).ttf", "fonts/font (2).ttf",     
"fonts/font (3).ttf", "fonts/font (4).ttf", "fonts/font (5).ttf", "fonts/font (6).ttf",    
"fonts/font (7).ttf", "fonts/font (8).ttf", "fonts/font (9).ttf", "fonts/font (10).ttf",     
"fonts/font (10).ttf"};

    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;

        //View row = null;

        if (v == null) {
            LayoutInflater inflater = Main.this.getLayoutInflater();
             v = inflater.inflate(R.layout.genre_list, parent, false);
             TextView item = (TextView)v.findViewById(R.id.txtListText);
             /*switch(position) {
             case 0:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (1).ttf");item.setTypeface(tf);
                  break;
             case 1:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (2).ttf"); item.setTypeface(tf);
                 break;
             case 2:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (3).ttf"); item.setTypeface(tf);
                 break;
             case 3:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (4).ttf"); item.setTypeface(tf);
                 break;
             case 4:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (5).ttf"); item.setTypeface(tf);
                 break;
             case 5:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (6).ttf"); item.setTypeface(tf);
                 break;
             case 6:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (7).ttf"); item.setTypeface(tf);
                 break;
             case 7:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (8).ttf"); item.setTypeface(tf);
                 break;
             case 8:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (9).ttf"); item.setTypeface(tf);
                 break;
             case 9:
                  tf = Typeface.createFromAsset(getAssets(),"fonts/font (10).ttf"); item.setTypeface(tf);
                 break;
             default:
                 tf = Typeface.createFromAsset(getAssets(),"fonts/font (10).ttf"); item.setTypeface(tf);
         }*/
        }

        TextView item = (TextView)v.findViewById(R.id.txtListText);
        item.setText(Genres[position]);
        tf = Typeface.createFromAsset(getAssets(),typefaceArr[position]);
        item.setTypeface(tf);
        // Declare and define the TextView, "icon." This is where
        // the icon in each row will appear.


        return v;
    }
于 2013-01-17T10:51:41.037 回答