Android 当textview是listview的一部分时,如何更改字体类型
我有一个列表视图,如下所示:Android 当textview是listview的一部分时,如何更改字体类型,android,listview,fonts,textview,Android,Listview,Fonts,Textview,我有一个列表视图,如下所示: lv1 = (ListView) findViewById(R.id.ListView01); ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map = new HashMap<String, String>(); map
lv1 = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("product", "Bread");
map.put("quantity", "1");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Books for mom");
map.put("quantity", "14");
map.put("unit", "pcs");
mylist.add(map);
map = new HashMap<String, String>();
map.put("product", "Mineral water");
map.put("quantity", "2");
map.put("unit", "l");
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.grlists,
new String[] {"product", "quantity", "unit"}, new int[] {R.id.CPRODUCT, R.id.CQUANTITY, R.id.CUNIT});
lv1.setAdapter(mSchedule);
如何以编程方式更改文本视图的字体类型?由于Dmytro Danylyk没有添加他的解决方案作为答案,因此我添加该解决方案以结束此问题。然而,在过去的几个月里,我已经掌握了这一领域,所以我在他发布的帖子中添加了一些不同的答案 首先,我们需要在listview中显示数组。此listview基于从阵列接收项目的自定义适配器构建。在这个自定义适配器中,我们可以对listview的每一行中的项执行任何需要的操作,无论我们讨论的是哪种类型的元素。无论是文本视图、图像视图还是复选框等 让我们假设列表行中有5个元素:5个文本视图。填充5个阵列后,我们将适配器定义为:
adapter = new ListViewCustomAdapter(Calllogs.this, arr_calllog_name, arr_calllog_phone, arr_calllog_type,arr_calllog_duration, arr_calllog_date);
下面是如何将适配器设置为listview的:
lv1.setAdapter(adapter);
最后,我们需要创建ListViewCustomAdapter(根据需要命名):
公共类ListViewCustomAdapter扩展了BaseAdapter
{
公共字符串标题[];
公共字符串描述[];
ArrayList arr_calllog_name=new ArrayList();
ArrayList arr_calllog_phone=new ArrayList();
ArrayList arr_calllog_type=new ArrayList();
ArrayList arr_calllog_date=new ArrayList();
ArrayList arr_calllog_duration=new ArrayList();
公共活动语境;
公共场所;充气机;
public ListViewCustomAdapter(活动上下文、ArrayList arr_calllog_名称、ArrayList arr_calllog_电话、ArrayList arr_calllog_类型、ArrayList arr_calllog_持续时间、ArrayList arr_calllog_日期){
超级();
this.context=上下文;
this.arr\u calllog\u type=arr\u calllog\u type;
this.arr\u calllog\u name=arr\u calllog\u name;
this.arr\u calllog\u phone=arr\u calllog\u phone;
this.arr\u calllog\u date=arr\u calllog\u date;
this.arr\u calllog\u duration=arr\u calllog\u duration;
this.inflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u inflater\u SERVICE);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回arr_calllog_name.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
公共静态类视图持有者
{
TextView-txtViewTitle;
TextView txtViewDescription;
TextView-txtDate;
TextView txtDuration;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视窗座;
if(convertView==null)
{
holder=新的ViewHolder();
convertView=充气机。充气(R.layout.calllog_行,空);
holder.txtViewTitle=(TextView)convertView.findViewById(R.id.calllogquery_name);
holder.txtViewDescription=(TextView)convertView.findViewById(R.id.calllogquery\u电话);
holder.txtDate=(TextView)convertView.findViewById(R.id.calllogquery_日期);
holder.txtDuration=(TextView)convertView.findViewById(R.id.calllogquery_duration);
convertView.setTag(支架);
}
其他的
holder=(ViewHolder)convertView.getTag();
int teljes=Integer.parseInt(arr_calllog_duration.get(position));
int h=teljes/3600;
int mar_h=teljes-h*3600;
int m=mar_h/60;
int s=mar_h-m*60;
串hh,mm,ss;
如果(h<10){hh=“0”+整数.toString(h);}
else{hh=Integer.toString(h);}
如果(m<10){mm=“0”+整数.toString(m);}
else{mm=Integer.toString(m);}
如果(s<10){ss=“0”+整数.toString(s);}
else{ss=Integer.toString(s);}
字符串dur_edited=hh+“:“+mm+”:“+ss;
holder.txtViewTitle.setText(arr_calllog_name.get(position));
holder.txtViewDescription.setText(arr_calllog_phone.get(position));
holder.txtDate.setText(arr_calllog_date.get(position));
holder.txtDuration.setText(dur_编辑);
返回视图;
}
}
正如您所看到的,这个适配器有5个参数,就像我们的5个阵列一样。这就是全部。
哦,我们还需要将适配器定义为ListViewCustomAdapter代码>第一个
您可以将此适配器类放在单独的类中,也可以放在您的活动中,在onCreate()方法之外。您希望列表行从一开始就使用此字体,还是在创建列表后更改此字体?这是您的答案,请使用搜索。斯卢基安:我希望他们有一个默认的自定义字体类型,但我还想给用户添加一个选项来更改它!请加上这个作为答案,这样我就可以接受了。
adapter = new ListViewCustomAdapter(Calllogs.this, arr_calllog_name, arr_calllog_phone, arr_calllog_type,arr_calllog_duration, arr_calllog_date);
lv1.setAdapter(adapter);
public class ListViewCustomAdapter extends BaseAdapter
{
public String title[];
public String description[];
ArrayList<String> arr_calllog_name = new ArrayList<String>();
ArrayList<String> arr_calllog_phone = new ArrayList<String>();
ArrayList<String> arr_calllog_type = new ArrayList<String>();
ArrayList<String> arr_calllog_date = new ArrayList<String>();
ArrayList<String> arr_calllog_duration = new ArrayList<String>();
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAdapter(Activity context, ArrayList<String> arr_calllog_name, ArrayList<String> arr_calllog_phone, ArrayList<String> arr_calllog_type, ArrayList<String> arr_calllog_duration, ArrayList<String> arr_calllog_date) {
super();
this.context = context;
this.arr_calllog_type = arr_calllog_type;
this.arr_calllog_name = arr_calllog_name;
this.arr_calllog_phone = arr_calllog_phone;
this.arr_calllog_date = arr_calllog_date;
this.arr_calllog_duration = arr_calllog_duration;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr_calllog_name.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
TextView txtViewTitle;
TextView txtViewDescription;
TextView txtDate;
TextView txtDuration;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.calllog_row, null);
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.calllogquery_name);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.calllogquery_phone);
holder.txtDate = (TextView) convertView.findViewById(R.id.calllogquery_date);
holder.txtDuration = (TextView) convertView.findViewById(R.id.calllogquery_duration);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
int teljes = Integer.parseInt(arr_calllog_duration.get(position));
int h = teljes / 3600;
int mar_h = teljes - h * 3600;
int m = mar_h / 60;
int s = mar_h - m * 60;
String hh, mm, ss;
if (h < 10) { hh = "0" + Integer.toString(h); }
else { hh = Integer.toString(h); }
if (m < 10) { mm = "0" + Integer.toString(m); }
else { mm = Integer.toString(m); }
if (s < 10) { ss = "0" + Integer.toString(s); }
else { ss = Integer.toString(s); }
String dur_edited = hh + ":" + mm + ":" + ss;
holder.txtViewTitle.setText(arr_calllog_name.get(position));
holder.txtViewDescription.setText(arr_calllog_phone.get(position));
holder.txtDate.setText(arr_calllog_date.get(position));
holder.txtDuration.setText(dur_edited);
return convertView;
}
}