Fuente personalizada en android ListView

Estoy usando una fuente personalizada en toda mi aplicación (que, por cierto, he descubierto frustrantemente que usted tiene que aplicar mediante programación a mano a CADA control!), Y tengo que aplicarlo a un listview. El problema es que no puedo ver dónde había fijado el textview usado en la fuente de la lista a mi fuente de encargo (como nunca instáncielo – eso es todo tomado cuidado por el adaptador).

Lo que me gustaría idealmente es poder utilizar un adaptador como este:

new ArrayAdapter(Context context, TextView textView, List<T> objects) 

De esa manera podría hacer: textView.setTypeface antes de rellenar mi lista. ¿Alguien sabe si hay una manera de hacer algo en este sentido?

No puede hacerlo de esa manera porque el recurso de vista de texto que pasa al ArrayAdapter se inflama cada vez que se utiliza.

Debe crear su propio adaptador y proporcionar su propia vista.

Un ejemplo para su adaptador podría ser

 public class MyAdapter extends BaseAdapter { private List<Object> objects; // obviously don't use object, use whatever you really want private final Context context; public CamAdapter(Context context, List<Object> objects) { this.context = context; this.objects = objects; } @Override public int getCount() { return objects.size(); } @Override public Object getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Object obj = objects.get(position); TextView tv = new TextView(context); tv.setText(obj.toString()); // use whatever method you want for the label // set whatever typeface you want here as well return tv; } 

}

Y entonces usted podría fijar eso como tal

 ListView lv = new ListView(this); lv.setAdapter(new MyAdapter(objs)); 

Esperemos que eso te ayude.

Si no desea crear una nueva clase puede reemplazar el método getView al crear su adaptador, este es un ejemplo de un simpleAdapter con título y subtítulo:

 Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf"); Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf"); SimpleAdapter adapter = new SimpleAdapter(this, items,R.layout.yourLvLayout, new String[]{"title", "subtitle" }, new int[] { R.id.rowTitle, R.id.rowSubtitle }){ @Override public View getView(int pos, View convertView, ViewGroup parent){ View v = convertView; if(v== null){ LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v=vi.inflate(R.layout.yourLvLayout, null); } TextView tv = (TextView)v.findViewById(R.id.rowTitle); tv.setText(items.get(pos).get("title")); tv.setTypeface(typeBold); TextView tvs = (TextView)v.findViewById(R.id.rowSubtitle); tvs.setText(items.get(pos).get("subtitle")); tvs.setTypeface(typeNormal); return v; } }; listView.setAdapter(adapter); 

Donde items es tu ArrayList de Maps

Espero que ayude

Parece que el constructor está equivocado

Cámbielo a:

 public MyAdapter (Context context, List<Object> objects) { this.context = context; this.objects = objects; } 

funciono bien para mi.

Prueba de este modo para arrayadapters ::

 Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf"); timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor) { public View getView(int pos, View convertView, android.view.ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.floorrow, null); } TextView tv = (TextView)v.findViewById(R.id.txt); tv.setText(flor.get(pos)); tv.setTypeface(typeNormal); return v; }; }; lv_building.setAdapter(timearray); 

Además de la respuesta de Moisés Olmedo – una variante alternativa sin crear una nueva clase:

  tf = Typeface.createFromAsset(getAssets(), fontPath); recordsAdapter = new SimpleCursorAdapter(this, R.layout.item1, cursor, from, to); recordsAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (columnIndex == 1) { final TextView tv = (TextView) view; tv.setTypeface(tf); } return false; } }); 

Primero copie y pegue los archivos de fuentes en la carpeta de recursos / fuentes. A continuación, identifique la vista de texto.

  Typeface font=Typeface.createFromAsset(activity.getAssets(), "fonts/<font_file_name>.ttf"); holder.text.setTypeface(font); holder.text.setText("your string variable here"); 

Puede configurar el adaptador base como sigue los pasos Puede serle ayuda

Definir su adaptador base

 public class LessonAdapter extends BaseAdapter { private Context mContext; public LessonAdapter(Context mContext, ArrayList<String> titles) { super(); this.mContext = mContext; } public int getCount() { // TODO Auto-generated method stub if (titles!=null) return titles.size(); else return 0; } public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = convertView; try { if (v == null) { v = inflater.inflate(R.layout.lesson_item, null); } TextView title = (TextView) v.findViewById(R.id.title); Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Rabiat_3.ttf"); title.setTypeface(tf); title.setText(titles.get(position).toString()); } catch (Exception e) { Log.d("searchTest", e.getMessage()); } return v; } } 

Por TypeFace Método usted puede fijar su fuente agrega la carpeta "fuentes" en activos entonces agrega su fuente en la carpeta de "fuentes"

Luego para configurar su adaptador

 adapter = new LessonAdapter(LessonsTitle.this, titles); setListAdapter(adapter); 

Holder.txt_name.setTypeface (Typeface.createFromAsset (activity.getAssets (), "fuente / nasimbold.ttf"));

  • "La tipografía nativa no se puede hacer" sólo para algunas personas
  • ¿Typeface.createFromAsset () cache?
  • Excepción de puntero nulo al importar una fuente en android
  • Fuente de Gujarati
  • ¿Cómo puedo configurar Typeface en la barra de acción?
  • Utilice Typeface con EditText en el widget
  • Recurso de fuente en Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.