2017-08-06 179 views
0

我正在尝试更改我的listview字体。我创建了assets/fonts文件夹。我在此文件夹中添加了OpenSans-Regular.ttf。我在我的Adapter类中添加了typeface行。 适配器:Android自定义字体渲染问题

public class Adapter extends BaseAdapter { 

    List<String> listFunds; 
    Typeface typeface; 


    Context mContext; 

    public Adapter(Context mContext, List<String> listFunds) { 
     this.mContext = mContext; 
     this.listFunds = listFunds; 

    } 

    public int getCount() { 
     return listFunds.size(); 
    } 

    public Object getItem(int arg0) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View arg1, ViewGroup viewGroup) { 

     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.list_row, viewGroup, false); 
     TextView textView = (TextView) row.findViewById(R.id.textView); 

     textView.setText(listFunds.get(position)); 
     typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/"+"OpenSans-Regular.ttf"); 
     textView.setTypeface(typeface); 


     return row; 
    } 
} 

然而,字体似乎不应该的。我尝试了很多字体,但没有改变。

我的应用程序似乎是这样的:

screenshot of my app

但是,字体看起来应该像这样 openSans-Regular

+0

您是在物理设备上还是在仿真器上运行应用程序?在Android模拟器上的 – Jonas

+0

。 @Jonas – heyaa

+1

请注意,仿真器在正确渲染图形时遇到麻烦。我强烈建议您在物理设备上运行应用程序。 – Jonas

回答

0

我建议你把这个typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/"+"OpenSans-Regular.ttf");在构造函数中的效益分析。它也可能是造成你问题的原因之一。或者,也许可以改变你使用的字体文件,对我来说它看起来也可能是与英文字符不同的语言。

+0

感谢您的快速回复!我也试过这个。正如@Jonas所说,模拟器是一个问题。 – heyaa

+0

太棒了。至少它有效 – Mohale