我有一个扩展到ListActivity的类,我想为其设置自定义字体。任何人都可以带我走过这个吗?我知道如何使用TextView的字体,但不知道ListActivity的字体。
public static class PunjabiBooks extends ListActivity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[] mathList = new String[] {"Gurmukh Soch", "Books (Punjabi)", "Books (Hindi)", "Books (English)",
"Annual Report"};
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mathList));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
if (keyword.equals("Gurmukh Soch")){
x = 0;
try{
Class ourClass = Class.forName("sukrit.trust.PublicationsTab$WebActivity");
Intent ourIntent = new Intent(PunjabiBooks.this, ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}else if(keyword.equals("Books (Punjabi)")){
try{
Class ourClass = Class.forName("sukrit.trust.Publications$PunjabiBooks");
// Intent ourIntent = new Intent(Publications.this, ourClass);
//startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}else if(keyword.equals("Books (Hindi)")){
try{
Class ourClass = Class.forName("sukrit.trust.PublicationsTab$HindiBooks");
// Intent ourIntent = new Intent(Publications.this, ourClass);
// startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}else if(keyword.equals("Books (English)")){
try{
Class ourClass = Class.forName("sukrit.trust.PublicationsTab$EnglishBooks");
// Intent ourIntent = new Intent(Publications.this, ourClass);
// startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}else if(keyword.equals("Annual Report")){
try{
Class ourClass = Class.forName("sukrit.trust.PublicationsTab$AnnualReport");
// Intent ourIntent = new Intent(Publications.this, ourClass);
// startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
ListView
本身并没有任何文字可以使用自定义字体。您需要处理提供此文本的类:Adapter
。您的适配器负责在ListView
中创建视图,包括(在这种情况下)相关的TextView
。您需要创建一个自定义适配器(可能来自ArrayAdapter<String>
),您可以在其中覆盖getView
方法。最简单的解决方案可能是简单地调用父实现的getView
,然后使用TextView
从android.R.layout.simple_list_item_1
布局中找到相关的View.findViewById
(您可以查找它,但是我相信相关的id是text1
),然后设置你已经知道如何做的自定义字体。这可能看起来像:
public class CustomAdapter extends ArrayAdapter<String> {
// Appropriate Constructor here
// ...
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
// Do your textView.setTypeface( ... ) stuff here
}
}