如何动态更改ListView的字体大小?

时间:2010-07-12 12:37:01

标签: android android-listview

如何动态更改ListView的字体大小?

1 个答案:

答案 0 :(得分:1)

使用自定义ListAdapter,覆盖getView,在初始化适配器时设置参数(textSize)...

public class MyListAdapter extends ArrayAdapter<String> {
    private String[] stringArray = null;
    private int textSize,itemLayout;
    public MyListAdapter(Context context, 
            String[] objects,int textSize) {
        super(context, R.layout.la_item, objects);
        stringArray = objects;
        itemLayout = R.layout.la_item;
        this.textSize = textSize;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
        {
            LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(itemLayout, null);
        }
        TextView tv = (TextView)convertView.findViewById(R.id.itemText);
        tv.setTextSize(textSize); // SET THE TEXT SIZE!
        tv.setText(stringArray[position]);
        return convertView;
    }

}

R.layout.la_item 是一个简单的线性布局,带有TextView ....

请参阅this有关如何使用ListAdapter ...

的信息