How to change font type and color in this listview

徘徊边缘 提交于 2020-01-06 03:15:26

问题


I have two string arrays: array1 and array2.

How can I change the font type?
How can I change the color for some element of the array (for example the first and the fifth element)?
My font type is in 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);

Here is my 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>

回答1:


It is convenient to make custom adapter for that. Like this:--

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;

    }
}



回答2:


Create a CustomAdapter and in that you have the getView() so there if you want to change the listview font use this :

For changing font :

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

& Color:

    listview1.setTextColor(Color.BLACK);


来源:https://stackoverflow.com/questions/20801286/how-to-change-font-type-and-color-in-this-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!