简体   繁体   English

如何使用SimpleAdapter更改textview作为listview的一部分时的字体

[英]How to change the font of textview when it is a part of listview using SimpleAdapter

Following is my code : 以下是我的代码:

And I wanna change the font of a Textview whose id is 'name12'. 我想更改ID为“ name12”的Textview的字体。 Need Help. 需要帮忙。 Thank you in advance. 先感谢您。

String rid = jObj.getString(TAG_RID);
String name = jObj.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_RID, rid);
map.put(TAG_NAME, name);
oslist.add(map);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_v, new String[] { TAG_NAME }, new int[] { R.id.name12});
l1.setAdapter(adapter);

you can extend TextView class and set font inside. 您可以extend TextView类并在其中设置字体。

After that you can use this TextView in R.layout.list_v 之后,您可以在R.layout.list_v使用此TextView

public class TextViewWithFont extends TextView {

    public TextViewWithFont(Context c) {
        this(c, null);
    }

    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(Typeface.createFromAsset(context.getAssets(), "fontname.ttf"));
    }

    public TextViewWithFont(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

}

The better way to achieve what you want is to create custom adapter. 实现您想要的更好的方法是创建自定义适配器。 Which will provide you more control. 它将为您提供更多控制。

If you want simple adapter to use any how, Do the following: 如果您希望简单的适配器使用任何方式,请执行以下操作:

public class CustomAdapter extends SimpleAdapter {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        Object item = getItem(position);
        //iterate to this view and it's child and if it's an instance of textview set the color 
        ColorStateList color = //get color for item;
        text.setTextColor(color);
        return v;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM