Android ExpandableListView中的自定义字体

Android ExpandableListView中的自定义字体,android,listview,fonts,expandablelistview,Android,Listview,Fonts,Expandablelistview,我想为我的一个textview制作自定义字体,但我仍在努力 cannot resolve method getAssets() 以下是代码的一部分: @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final String childText = (St

我想为我的一个textview制作自定义字体,但我仍在努力

cannot resolve method getAssets()
以下是代码的一部分:

    @Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String)getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, parent, false);
    }
    Utils.setFontAllView((ViewGroup)convertView);

    TextView txtListChild   = (TextView)convertView.findViewById(R.id.txtcardname);
    txtListChild.setTypeface(null, Typeface.BOLD);

    TextView kategoria  = (TextView)convertView.findViewById(R.id.kategoria);
    kategoria.setText(this.KATEGORIE[numerKategori]);


    custom_font = Typeface.createFromAsset(getAssets(), "fonts/capture.ttf");
    txtListChild.setTypeface(custom_font);
我想用以下代码来实现:

custom_font = Typeface.createFromAsset(getAssets(), "fonts/capture.ttf");
        txtListChild.setTypeface(custom_font);

将您的代码更新到此版本,它将起作用:

custom_font = Typeface.createFromAsset(mContext.getAssets(), "fonts/capture.ttf");
没有视图的上下文,您无法访问资产。所以上下文是强制性的


您可以在自定义textview类中定义字体,而不是在getView方法中设置字体,使用此方法将占用更少的内存,并且您的应用程序性能将更好。

您可以使用上下文调用getAssets。使用以下命令:

custom_font = Typeface.createFromAsset(this._context.getAssets(), "fonts/capture.ttf");

祝你好运。

因为你想从你的代码中将它使用到适配器中

执行以下更改

使用上下文

确保在“字体”下的资产文件夹中有capture.ttf文件

 Context context = null;
            Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/capture.ttf");
      txtListChild.setTypeface(custom_font);
检查文件捕获的路径。ttf

 Context context = null;
            Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/capture.ttf");
      txtListChild.setTypeface(custom_font);