繁体   English   中英

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

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

以下是我的代码:

我想更改ID为“ name12”的Textview的字体。 需要帮忙。 先感谢您。

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);

您可以extend TextView类并在其中设置字体。

之后,您可以在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);
    }

}

实现您想要的更好的方法是创建自定义适配器。 它将为您提供更多控制。

如果您希望简单的适配器使用任何方式,请执行以下操作:

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