Cómo agregar fuentes externas a la aplicación Android


Estaba buscando algunas fuentes elegantes para mi aplicación Android. pero el problema es cómo puedo hacer que mi aplicación Android sea compatible con fuentes externas.

Gracias.

Author: bHaRaTh, 2011-04-12

7 answers

Necesita crear la carpeta fonts en la carpeta assets en su proyecto y poner su TTF en ella. Luego en tu Actividad onCreate ()

TextView myTextView=(TextView)findViewById(R.id.textBox);
Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
myTextView.setTypeface(typeFace);

Tenga en cuenta que no todos los TTF funcionarán. Mientras experimentaba, funcionó solo para un subconjunto(en Windows aquellos cuyo nombre está escrito en mayúsculas).

 145
Author: Zelimir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-10 18:36:52

Puede usar la vista de texto personalizada para toda la aplicación con fuente personalizada aquí hay un ejemplo para eso

public class MyTextView extends TextView {

   Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
   Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);

   public MyTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }

   public MyTextView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }

   public MyTextView(Context context) {
       super(context);
   }

   public void setTypeface(Typeface tf, int style) {
       if (style == Typeface.BOLD) {
           super.setTypeface(boldTypeface/*, -1*/);
       } else {
           super.setTypeface(normalTypeface/*, -1*/);
       }
   }
}
 11
Author: Jabbir Basha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-17 10:20:39

Cree una carpeta llamada fonts en la carpeta assets y agregue el fragmento del siguiente enlace.

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
textview.setTypeface(tf);
 9
Author: Jana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-12 11:17:36

Para implementar necesita usar tipo de letra ir a través de la muestra a continuación

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
   if (view instanceof TextView) 
   {
      TextView textView = (TextView) view;
      textView.setTypeface(typeface);
      }
   }
}
 7
Author: Jabbir Basha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-30 15:19:16

La forma más sencilla de lograr esto es el paquete de la fuente deseada(s) con tu solicitud. Para hacer esto, simplemente cree una carpeta assets / en la raíz del proyecto, y poner sus fuentes (en TrueType, o TTF, forma) en activo. Puede, por ejemplo, crear assets / fonts / y poner su Archivos TTF ahí.

Entonces, necesitas decirle a tus widgets que usen esa fuente. Desafortunadamente, ya no puede usar layout XML para esto, ya que el XML no sabe acerca de cualquier fuente que puede haber escondido como un activo de la aplicación. En su lugar, debe realizar el cambio en el código Java, llamando Tipografía.createFromAsset (getAssets (), " fonts/HandmadeTypewriter.ttf"), luego tomar el objeto de tipo de letra creado y pasarlo a su TextView vía setTypeface ().

Para más referencia aquí está el tutorial donde obtuve esto:

Http://www.androidguys.com/2008/08/18/fun-with-fonts /

 5
Author: bHaRaTh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-17 17:52:11

Recomiendo este enfoque es muy agradable agregar el nombre de la fuente personalizada en typeface a styles.xml y poner su conjunto de fuentes en la carpeta assets.

 2
Author: ar-g,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-03 10:37:42

Un punto más además de las anteriores respuestas. Cuando se utiliza una fuente dentro de un fragmento, la instanciación del tipo de letra debe hacerse en el método onAttach (override) como se indica a continuación:

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf");
}

Razón :
Hay un corto lapso de tiempo antes de que un fragmento se adjunta a una actividad. Si se llama al método CreateFromAsset antes de adjuntar fragment a una actividad, se produce un error.

 1
Author: Akil Adeshwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-28 23:50:14