Пользовательский шрифт для ListView в Android?
У меня есть класс, который расширен до 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();
}
}
}
1 ответ
ListView
по сути не имеет никакого текста, для которого можно использовать собственный шрифт. Вам нужно разобраться с классом, который предоставляет этот текст: Adapter
, Ваш адаптер отвечает за создание представлений в ListView
в том числе (в этом случае) TextView
обсуждаемый. Вам нужно будет создать собственный адаптер (вероятно, производный от ArrayAdapter<String>
) где вы переопределяете getView
метод. Самым простым решением, вероятно, было бы просто вызвать родительскую реализацию getView
а затем найти соответствующий TextView
от android.R.layout.simple_list_item_1
макет с использованием View.findViewById
(вы можете посмотреть, но я считаю, что соответствующий идентификатор 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
}
}