在Android中的ListActivity上设置字体

时间:2011-07-11 23:26:59

标签: android fonts

我正在尝试在Android中的列表活动中设置自定义字体。单击项目后,我可以成功完成此操作,如下所示:

@Override
protected void onListItemClick(ListView listView, View listItemView,
        int position, long id) {

        int childCount = listView.getChildCount();

        for (int i = 0; i < childCount; i++) {
            TextView c = (TextView) listView.getChildAt(i);
            c.setTypeface(mTypeFace);
        }

但是,在单击项目之前,分配字体不会产生任何影响:

    //
    // Create the adapter to display the choice list
    //
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.simple_list_item_single_choice_custom,
            mAppState.mAnswerArray) {
    };


    /* attach the adapter to the ListView */
    setListAdapter(adapter);


    ListView v = getListView();


    int childCount = v.getChildCount();

    for (int i = 0; i < childCount; i++) {
        TextView c = (TextView) v.getChildAt(i);
        c.setTypeface(mTypeFace);
    }

调试显示在初始设置中,ListView v没有子节点,尽管它们出现在GUI中并且mAnswerArray有子节点。

知道问题可能是什么?

1 个答案:

答案 0 :(得分:1)

设置viewBinder并覆盖适配器中的setViewValue,如下所示:

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        if(columnIndex == 7){
            TextView textView = (TextView) view;
            textView.setTypeface(gotham_book);
        }
     }
}