Android更改ListView字体 [英] Android change ListView font
问题描述
似乎是一个简单的问题,但是由于我是Android开发的新手,所以我对Android ListViews几乎一无所知.以下是我在项目中用于ListView的代码.
Seems to be a simple question, but since I am new to Android development I have very little idea about Android ListViews. Following is the code I have used for a ListView in my project.
/*Listview code starts*/
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(values) );
listAdapter = new ArrayAdapter<String>(this, R.layout.list1, values);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
int itemPosition = position;
String itemValue = (String) mainListView.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
/*Listview code ends*/
我希望更改ListView文本的字体.我怎么做?到处阅读以使用自定义适配器,但听不懂.谁能帮我提供代码?
I wish to change the Font of my ListView text. How do I do that? Read everywhere to use a custom Adapter, but did not understand. Can anyone help me with the code?
推荐答案
如果您想创建一个新的ArrayAdapter
并通过覆盖getView()
方法访问ListView
中的项目,请执行以下步骤.请查看 Adapter#getView .. 此处和此处是有关自定义ListView
的很好的教程.
If you want to go by the way with creating a new ArrayAdapter
and access to the items inside the ListView
by overriding the getView()
method. Please have a look at Adapter#getView .. Here and Here are good tutorials about customizing the ListView
.
示例自定义ArrayAdapter
会像这样.
public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public CustomArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_mobile, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
// Customization to your textView here
textView.setText("Hello");
textView.setTypeface(my_custom_typeface);
textView.setTextSize(20);
return rowView;
}
}
您可以像这样创建一个新的CustomArrayAdapter
.
And you can create a new CustomArrayAdapter
by like this.
CustomArrayAdapter my_adapter = new CustomArrayAdapter();
setListAdapter(my_adapter);
Ref: Android TextView方法.
这篇关于Android更改ListView字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!