如何将自定义字体分配给listView中的已解析数据?

时间:2014-02-20 15:15:21

标签: android listview jsoup custom-font

我只是用jsoup解析了一些数据并在listview中显示它们 但我想改变解析数据的字体? 我应该创建一个自定义的lv? 如何?

public class MainActivity extends Activity {
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, titleList);
}

public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {
        Document doc;
        try {
            doc = Jsoup.connect("http://tinyez.net//").get();
            title = doc.select("div.post.lit");
            title1 = title.select("h2");
            for (Element titles : title1) {
                titleList.add(titles.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        lv.setAdapter(adapter);
    }
}
}

2 个答案:

答案 0 :(得分:1)

步骤1:创建一个名称为&#34;文件夹&#34;的文件夹in&#34; assets&#34;文件夹中。

步骤2:下载要应用于文本的字体(文件应为.ttf格式)。

步骤3:从MainActivity中将String添加到List并将其传递给CustomAdapter并将适配器添加到ListView。

public class MainActivity extends Activity {
List<String> listString ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String[] stringArray = new String[]{"Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2"};
    listString = new ArrayList<String>();

    for(int i=0; i<stringArray.length;i++){
        listString.add(stringArray[i]);
    }
    ListView listView = (ListView) findViewById(R.id.listView);

    CustomAdapter customAdapter = new CustomAdapter(this,listString);
    listView.setAdapter(customAdapter);
}

}

步骤4:创建类CustomAdapter并使用ArrayAdapter扩展它并执行下面提到的代码。

public class CustomAdapter extends ArrayAdapter<String>{

List<String> listString;
Activity context;
Typeface typeFace ;
public CustomAdapter(MainActivity mainActivity, List<String> listString) {
    super(mainActivity,R.layout.list_row,listString);
    this.context=mainActivity;
    this.listString=listString;
}
static class ViewHolder{
    public TextView textView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(vi==null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        vi = inflater.inflate(R.layout.list_row, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) vi.findViewById(R.id.textView);
        vi.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder) vi.getTag();
    typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/verdana.ttf");
    holder.textView.setTypeface(typeFace);
    holder.textView.setText(listString.get(position));

    return vi;
}

}

答案 1 :(得分:0)

您必须创建自定义ListView适配器。在getView()中,您可以设置TextView的类型面。

请参阅有关如何编写自定义适配器的信息:http://www.vogella.com/tutorials/AndroidListView/article.html