Cómo configurar la fuente personalizada para android listview?

Soy nuevo a android y usando la fuente de encargo para listview.I no sabía utilizar el tipo de letra en una visión de lista. También intenté con diversos ejemplos pero no puedo solucionar mi problema. Aquí está mi código

public class HomeScreen extends ListActivity { private static final int QUICK_START_INDEX =0; private static final int CUSTOM = 1; private static final int CALL_LIST = 2; private static final int CALENDAR = 3; private static final int TEMPLATES=4; private static final int USER_GUIDE = 5; static final String[] LIST = new String[] { "QuickStart", "Custom", "CallList", "Calendar","Templates","UserGuide"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new MobileArrayAdapter(this, LIST)); /*Typeface font = Typeface.createFromAsset(getAssets(), "earthkid.ttf"); */ } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); /*Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();*/ if(position==QUICK_START_INDEX){ startActivity(new Intent(HomeScreen.this,ConfDialer.class)); } } } 

MobileArrayAdapter.java

 public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_home, 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_home, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); // Change icon based on name String s = values[position]; System.out.println(s); if (s.equals("QuickStart")) { imageView.setImageResource(R.drawable.quick_strat); } else if (s.equals("Custom")) { imageView.setImageResource(R.drawable.customize); } else if (s.equals("CallList")) { imageView.setImageResource(R.drawable.call_list); } else if(s.equals("Calendar")){ imageView.setImageResource(R.drawable.calendar); } else if(s.equals("Templates")){ imageView.setImageResource(R.drawable.templates); } else{ imageView.setImageResource(R.drawable.user_guide); } return rowView; } } 

¿Podría alguien proporcionar una solución para esto.

Prueba esto…

Puede anular el getView () al crear su Adaptador en Actividad, por ejemplo de un SimpleAdapter con nombre y título .

 //global declaration ListAdapter adapter; adapter = SimpleAdapter(getActivity(), your_list, R.layout.your_list_items, new String[]{"Name", "Title"}, new int[]{R.id.name, R.id.title}){ @Override public View getView(int position, View convertView, ViewGroup parent) { v = convertView; if(v == null){ LayoutInflater li = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = li.inflate(R.layout.interview_list_items, null); TextView titleText= (TextView) v.findViewById(R.id.title); TextView nameText= (TextView) v.findViewById(R.id.name); childFont = Typeface.createFromAsset(getActivity().getAssets(),"fonts/Roboto-Light.ttf"); titleText.setTypeface(childFont); nameText.setTypeface(childFont); return super.getView(position, v, parent); } }; 

O intentarlo en la clase Adaptador como este …

 public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; private Typeface tf; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_home, values); this.context = context; this.values = values; this.tf = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); //initialize typeface here. } @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_home, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setTypeface(tf); // set typeface here textView.setText(values[position]); // Change icon based on name String s = values[position]; System.out.println(s); if (s.equals("QuickStart")) { imageView.setImageResource(R.drawable.quick_strat); } else if (s.equals("Custom")) { imageView.setImageResource(R.drawable.customize); } else if (s.equals("CallList")) { imageView.setImageResource(R.drawable.call_list); } else if(s.equals("Calendar")){ imageView.setImageResource(R.drawable.calendar); } else if(s.equals("Templates")){ imageView.setImageResource(R.drawable.templates); } else{ imageView.setImageResource(R.drawable.user_guide); } return rowView; } 

}

Estructura del directorio de aplicaciones

Introduzca aquí la descripción de la imagen

Utilizar este código

En tu MobileArrayAdapter

 class MobileArrayAdapter extends ArrayAdapter<CharSequence>{ Typeface tf; public MobileArrayAdapter(Context context, String[] values,String FONT) { tf = Typeface.createFromAsset(context.getAssets(), FONT); } 

Desde donde está llamando a su adaptador

 listAdapter = new MobileArrayAdapter(this, R.layout.custom_list_text, R.array.arrayName, "name_of_font.ttf"); 

Utilizar

 setTypeface(Typeface) 

En el objeto Textview.

Sólo tiene que cambiar la fuente en este diseño " R.layout.list_home ". Debido a que list_home es el diseño que funciona como una sola fila. Así que si lo cambia, también afectará al color / texto / estilo de listview .

Ponga un archivo de fuente personalizada .ttf en la carpeta de assets y utilice el código a continuación para cambiar la fuente

 text.setTypeface(Typeface.createFromAsset(getAssets(), "/*path to your font file */")); 

Camino sería el camino en los activos. Si el archivo está en la carpeta de activos sólo use el nombre de archivo con extensión.

Me gusta

  text.setTypeface(Typeface.createFromAsset(getAssets(), "arial.ttf")); 

Este es mi adaptador personalizado cómo llamar y qué parámetros se dan en mi actividad de llamada.

 public class CustomData extends ArrayAdapter<CharSequence> { Context context; int layoutResourceId; CharSequence data[] = null; Typeface tf; public CustomData(Context context, int layoutResourceId, CharSequence[] data, String FONT) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; tf = Typeface.createFromAsset(context.getAssets(), FONT); } } 
  • Evitar SwipeRefresh mientras se está acelerando en android
  • Cómo agregar márgenes por encima y por debajo de un ListView en Android?
  • ListView Selección de artículos Android
  • Focusable EditText dentro de ListView
  • Crear intención dentro del listview del evento setOnItemClickListener
  • Error: El método no estático 'findViewById (int)' no se puede referenciar desde un contexto estático
  • Cómo obtener el texto del elemento seleccionado listview cuando se hace clic en el botón AlertDialog
  • Disposición de la vista Lista de Android Similar a Google play
  • Bucle para cargar las imágenes de la lista una por una
  • No se ha encontrado ninguna vista para id en Android FragmentActivity con ListFragment
  • Android ListView problemas en Android 2.X
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.