自定义ExpandableListAdapter中的Android自定义字体

时间:2013-01-25 13:14:47

标签: android expandablelistview expandablelistadapter

我正在尝试在自定义可扩展列表视图中使用自定义字体。但是,当我尝试这样做时,最初,当Viewpager片段(我使用)加载时,ExpandableListView看不到文本和字体。

单击可展开列表视图中的空白文本后,将显示文本和字体。这个问题似乎只发生在自定义字体上。如果我使用任何其他自定义功能,如更改textview的颜色等,它运作良好。请帮助

请参阅附图以获取进一步参考: 1 2

我的自定义可扩展listadapter代码的相关部分:

public class MyExpandableListAdapter extends SimpleExpandableListAdapter {

    View cntView;
    LayoutInflater inflater;
    Context cxt;
    TextView tvGrp, tvChild;

    public MyExpandableListAdapter(Context context,
            List<? extends Map<String, ?>> groupData, int groupLayout,
            String[] groupFrom, int[] groupTo,
            List<? extends List<? extends Map<String, ?>>> childData,
            int childLayout, String[] childFrom, int[] childTo) {
        super(context, groupData, groupLayout, groupFrom, groupTo, childData,
                childLayout, childFrom, childTo);
        cxt=context;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        super.getGroupView(groupPosition, isExpanded, convertView, parent);
        View tmp;


        if (convertView == null) {  // if it's not recycled, initialize some attributes
            tmp=new View(cxt);
            inflater=(LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            tmp=inflater.inflate(R.layout.explistgroup, null);
        }else{
            tmp = (View)convertView;
        }

        tvGrp=(TextView) tmp.findViewById(R.id.rowTextView);
        tvGrp.setTypeface(FontLoader.loadTypeface(cxt,R.raw.roboto_regular));  // This does not work. It shows a blank textview initially. Only after I click it, the typeface and the text come into effect
        //tvGrp.setText("ambit"); -- This works well

        return tmp;

    }

3 个答案:

答案 0 :(得分:0)

尝试从资源加载自定义字体,例如:

Typeface tf = Typeface.createFromAsset(getAssets(), "<your font name>.ttf");
tvGrp.setTypeface(tf);

答案 1 :(得分:0)

好的,这是你需要做的事情: 1-在Activity中初始化一个公共静态变量,从而为可扩展列表视图使用自定义适配器,如下所示:MainListActivity:

public static Typeface typeFace;

2-现在像Natali在同一个Activity中提到的那样初始化它:

tf = Typeface.createFromAsset(getAssets(), "<your font name>.ttf");

3-现在按照Natali先生的指示在您的自定义适配器中使用以下内容,但稍作修改

tvGrp.setTypeface(MainListActivity.typeFace);

如果问题仍然存在,请尝试使用BaseExpandableListAdapter

扩展自定义适配器类

答案 2 :(得分:0)

您只创建一个视图,而不是为其设置文本。 xml中有text吗? 我认为这种方法应该是这样的:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    ViewGroup tmp = (ViewGroup)super.getGroupView(groupPosition, isExpanded, convertView, parent);
    tvGrp=(TextView) tmp.findViewById(R.id.rowTextView);
    tvGrp.setTypeface(FontLoader.loadTypeface(cxt,R.raw.roboto_regular));  // This does not work. It shows a blank textview initially. Only after I click it, the typeface and the text come into effect
    //tvGrp.setText("ambit"); -- This works well

    return tmp;

}

或者您创建和初始化而不是调用super。

这应该可以正常工作。

相关问题