Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置自定义字体时会减慢listview_Java_Android_Android Layout_Truetype_Android Fonts - Fatal编程技术网

Java 设置自定义字体时会减慢listview

Java 设置自定义字体时会减慢listview,java,android,android-layout,truetype,android-fonts,Java,Android,Android Layout,Truetype,Android Fonts,我在一个xml文件中有一些数据,将其放在/res/values/mydata.xml中。我想用自定义字体在列表视图中显示数据。在emulator中一切都很棒,但在真实设备(使用三星galaxy tab 10.1 2和android 4.0.3)中,滚动listview时速度太慢。实际上,它与默认字体配合使用效果很好,但在设置自定义字体时会出现问题 这是我的java代码: public class ShowFoodCalorie extends ListActivity { public void

我在一个xml文件中有一些数据,将其放在/res/values/mydata.xml中。我想用自定义字体在列表视图中显示数据。在emulator中一切都很棒,但在真实设备(使用三星galaxy tab 10.1 2和android 4.0.3)中,滚动listview时速度太慢。实际上,它与默认字体配合使用效果很好,但在设置自定义字体时会出现问题

这是我的java代码:

public class ShowFoodCalorie extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // reading data from xml file
    setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
            R.id.textView1,  getResources().getStringArray(R.array.food_cal)));
}
private class MyAdapter extends ArrayAdapter<String> {
    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.show_all, parent, false);
        String[] item = getResources().getStringArray(R.array.food_cal);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        try {
            Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");
            tv.setTypeface(font);
        } catch (Exception e) {
            Log.d("Alireza", e.getMessage().toString());
        }

        tv.setText(item[position]);
        return row;
    }
}
public类ShowFoodCalorie扩展了ListActivity{
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//从xml文件读取数据
setListAdapter(新的MyAdapter(此,android.R.layout.simple_列表_项_1,
R.id.textView1,getResources().getStringArray(R.array.food_-cal));
}
私有类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文上下文、int资源、int textViewResourceId、,
字符串[]字符串){
super(上下文、资源、textViewResourceId、字符串);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=(LayoutInflater)
getSystemService(上下文布局\充气机\服务);
视图行=充气机。充气(R.layout.show_all,parent,false);
String[]item=getResources().getStringArray(R.array.food_cal);
TextView tv=(TextView)row.findViewById(R.id.textView1);
试一试{
Typeface font=Typeface.createFromAsset(getAssets(),“myFont.ttf”);
设置字体(字体);
}捕获(例外e){
Log.d(“Alireza”,例如getMessage().toString());
}
tv.setText(项目[位置]);
返回行;
}
}
这是什么问题?与我的设备有关?任何解决方案都可以帮助我。
谢谢

你的问题是那句话:

Typeface font = Typeface.createFromAsset(getAssets(),"myFont.ttf");
您应该在适配器的构造函数中执行此操作,将
font
作为成员变量,而不仅仅是在
TextView
上使用该变量调用
setTypeface(font)

应防止
getView()
方法中的重载

另外,请阅读有关适配器的convertView/ViewHolder模式的信息,这也会提高性能

更新示例:

private class MyAdapter extends ArrayAdapter<String> {
    Typeface font;

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] string) {
        super(context, resource, textViewResourceId, string);
        font = Typeface.createFromAsset(context.getAssets(),"myFont.ttf");
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.show_all, parent, false);
        String[] item = getResources().getStringArray(R.array.food_cal);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        try {
            tv.setTypeface(font);
        } catch (Exception e) {
            Log.d("Alireza", e.getMessage().toString());
        }

        tv.setText(item[position]);
        return row;
    }
}
私有类MyAdapter扩展了ArrayAdapter{
字体;
公共MyAdapter(上下文上下文、int资源、int textViewResourceId、,
字符串[]字符串){
super(上下文、资源、textViewResourceId、字符串);
font=Typeface.createFromAsset(context.getAssets(),“myFont.ttf”);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=(LayoutInflater)
getSystemService(上下文布局\充气机\服务);
视图行=充气机。充气(R.layout.show_all,parent,false);
String[]item=getResources().getStringArray(R.array.food_cal);
TextView tv=(TextView)row.findViewById(R.id.textView1);
试一试{
设置字体(字体);
}捕获(例外e){
Log.d(“Alireza”,例如getMessage().toString());
}
tv.setText(项目[位置]);
返回行;
}
}

下面的链接提供了关于@WarrenFaith建议的convertView/ViewHolder模式的好信息。你能用一个例子更新吗?这样其他人就可以从中受益:)@sanjeev done!尽管我不鼓励使用这个旧代码并使用recyclerviewinstead@WarrenFaith实际上,我使用的是recyclerview,不幸的是,当我调用该类时从菜单上看,应用程序只是挂起,需要时间来打开活动。我在寻找解决方案时遇到了这个问题:)使用android:fontFamily从布局设置字体是否会降低性能?注意:我使用的是可下载的Google字体。。