如何更改此列表视图中的字体类型和颜色

时间:2013-12-27 13:07:55

标签: android android-listview textview android-fonts

我有两个字符串数组: array1 array2

如何更改字体类型?
如何更改数组中某些元素的颜色(例如第一个和第五个元素)?
我的字体类型是raw / price.ttf。

ListView listView1=(ListView) findViewById(R.id.listView1);

List<Map<String, String>> data = new ArrayList<Map<String, String>>();
    for (int i=0; i<array2.length; i++) {
        Map<String, String> datum = new HashMap<String, String>(2);
        datum.put("title", array1[i]);
        datum.put("subtitle", String.valueOf(array2[i]));
        data.add(datum);
    }

SimpleAdapter adapter3 = new SimpleAdapter(this, data,
                                   android.R.layout.simple_list_item_2,
                                   new String[] {"title", "subtitle"},
                                   new int[] {android.R.id.text1,
                                              android.R.id.text2});
listView1.setAdapter(adapter3);

这是我的xml

<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:scrollingCache="false">
    </ListView>

2 个答案:

答案 0 :(得分:0)

为此制作自定义适配器很方便。 像这样: -

private class MyListAdapter extends BaseAdapter {

    List<Map<String, String>> data ;
    LayoutInflater inflater;

    public MyListAdapter(Context context,List<Map<String, String>> data ) {
        // TODO Auto-generated constructor stub
        this.data  = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mDisplayedValues.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View convertView,
            ViewGroup parent) {
        // TODO Auto-generated method stub

        View v = convertView;
        ViewHolder holder;
        if (v == null) {
            v = inflater.inflate(R.layout.<your_list_row>, null);
            holder = new ViewHolder();
            holder.text1= (TextView) v
                    .findViewById(R.id.text1);
            holder.text2 = (TextView) v.findViewById(R.id.text2);

            v.setTag(holder);
        } else
            holder = (ViewHolder) v.getTag();
        holder.text1.setText("mytext1");
        holder.text2.setText("mytext2");
                    holder.text1.setTypeFace(--Your Typeface(font)---);

        return v;
    }

    class ViewHolder {
        TextView text1;
        TextView text2;

    }
}

答案 1 :(得分:0)

创建一个CustomAdapter,并且你有getView(),所以如果你想改变listview字体,请使用:

更改字体:

final Typeface font = Typeface.createFromAsset(assetManager, "price.TTF");
    listview1.setTypeface(font);

&安培;颜色:

    listview1.setTextColor(Color.BLACK);