0

当我更改当前活动上所有文本视图的字体时,除了 Textview 文本外,所有文本都发生了变化。

但是当我从 listview 的 itemclick 侦听器调用相同的函数时,字体会发生变化

        ((TextView) view).setTypeface(CreateTypeface);

如何设置列表视图的字体而无需单击它?

     final View rootView = findViewById(android.R.id.content);


public  void applyCustomFont(ViewGroup list) 
    {


         for (int i = 0; i < list.getChildCount(); i++) {
             View view = list.getChildAt(i);

             if (view instanceof ViewGroup) 
             {
                 applyCustomFont((ViewGroup) view);
             } else if (view instanceof TextView) 
             {
                 ((TextView) view).setTypeface(CreateTypeface);



         }
        }
     }



     lvfonts.setAdapter(new ArrayAdapter<String> (FontMUI.this,android.R.layout.simple_list_item_1, list));

     lvfonts.setOnItemClickListener(new  OnItemClickListener() 
     { 

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3)
                  {
                   applyCustomFont((ViewGroup)rootView);
                        }
                        } 
4

2 回答 2

1

使用此技巧设置列表视图的字体由于列表视图不是 Textview 的子类,因此无法直接访问列表视图的子类,您需要通过列表视图本身访问它们。

lvfonts.postDelayed(new Runnable() {
            @Override
            public void run() {
                applyCustomFont((ViewGroup)rootView);
                 // or call you function to change the Typeface from here 
            }
            }, 10);
于 2013-10-22T05:29:23.977 回答
1

您将需要创建一个用于填充 ListView 的自定义适配器。在该自定义适配器中,您将需要覆盖该getView()方法以在视图显示给用户之前对视图进行任何更改。

像这样的东西:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        ((TextView) convertView).setTypeface(CreateTypeface);
    }

    setupTextForPosition(convertView, position);
}
于 2013-10-21T13:58:27.273 回答