开发者

Setting font on ListActivity in Android

I'm trying to set a custom font on a list activity in Android. I can successfully do it once an item gets clicked, like so:

@Override
protected void onListItemClick(ListView listView, View listItemView,
        int p开发者_StackOverflowosition, long id) {

        int childCount = listView.getChildCount();

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

However, before the item is clicked, assigning a font has no impact:

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

Debugging show that in the initial setup, the ListView v has zero children, despite the fact they show up in the GUI and mAnswerArray has the children.

Any idea what the problem might be?


Set the viewBinder and override the setViewValue in your adapter, like so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜