tipo teclado para moto letra fuentes fuente como celular cambiar aplicacion android fonts

teclado - Android: quiere configurar fuentes personalizadas para toda la aplicación, no en tiempo de ejecución



como cambiar la letra del teclado del celular (11)

Android 8.0 (nivel de API 26) presenta una nueva característica, Fuentes en XML. Puede crear un archivo de familia de fuentes y establecerlo en styles.xml.

Para agregar fuentes como recursos, realice los siguientes pasos en Android Studio:

1.Haga clic con el botón derecho en la carpeta res y vaya a Nuevo> directorio de recursos de Android. Aparece la ventana Nuevo directorio de recursos.

2.En la lista Tipo de recurso, seleccione fuente y luego haga clic en Aceptar. Nota: El nombre del directorio de recursos debe ser fuente.

3.Agregue sus archivos de fuentes en la carpeta de fuentes.

Para crear una familia de fuentes, realice los siguientes pasos:

1.Haga clic con el botón derecho en la carpeta de fuentes y vaya a Nuevo> Archivo de recursos de fuentes. Aparece la ventana Nuevo archivo de recursos.

2. Ingrese el nombre del archivo y luego haga clic en Aceptar. El nuevo XML de recurso de fuente se abre en el editor.

3.Incluya cada archivo de fuente, estilo y atributo de peso en el elemento. El siguiente XML ilustra la adición de atributos relacionados con la fuente en el XML de recursos de fuentes:

<?xml version="1.0" encoding="utf-8"?> <font-family xmlns:android="http://schemas.android.com/apk/res/android"> <font android:fontStyle="normal" android:fontWeight="400" android:font="@font/lobster_regular" /> <font android:fontStyle="italic" android:fontWeight="400" android:font="@font/lobster_italic" /> </font-family>

Agregar fuentes al estilo

Abra el archivo styles.xml y establezca el atributo fontFamily en el archivo de fuente al que desea acceder.

<style name="customfontstyle" parent="@android:style/TextAppearance.Small"> <item name="android:fontFamily">@font/lobster</item> </style>

Fuente: fuentes en XML

¿Es posible configurar cualquier fuente personalizada en cada control de la aplicación? Y no necesariamente en tiempo de ejecución? (es decir, desde xml si es posible o solo una vez para toda la aplicación en el archivo JAVA)

Puedo establecer la fuente para un control desde este código.

public static void setFont(TextView textView) { Typeface tf = Typeface.createFromAsset(textView.getContext() .getAssets(), "fonts/BPreplay.otf"); textView.setTypeface(tf); }

Y el problema con este código es que debería llamarse para cada control. Y quiero llamar a este o a cualquier método similar una vez, o si es posible establecer la propiedad en xml. ¿Es posible?


Desafortunadamente, Android no proporciona la manera rápida, fácil y limpia que está buscando para cambiar la fuente de toda su aplicación. Pero recientemente investigué este asunto y creé algunas herramientas que te permiten cambiar la fuente sin ningún tipo de codificación (puedes hacerlo a través de xml, estilos e incluso apariencias de texto). Se basan en soluciones similares a las que se ven en las otras respuestas aquí, pero permiten mucha más flexibilidad. Puede leerlo al respecto en este blog y ver el proyecto github here .

Aquí hay un ejemplo de cómo aplicar estas herramientas. Pon todos tus archivos de fuentes en assets/fonts/ . Luego, declare esas fuentes en un archivo xml (por ejemplo, res/xml/fonts.xml ) y cargue este archivo temprano en su aplicación con TypefaceManager.initialize(this, R.xml.fonts); (p. ej., en la clase onCreate of your Application). El archivo xml se ve así:

<?xml version="1.0" encoding="utf-8"?> <familyset> <!-- Some Font. Can be referenced with ''someFont'' or ''aspergit'' --> <family> <nameset> <name>aspergit</name> <name>someFont</name> </nameset> <fileset> <file>Aspergit.ttf</file> <file>Aspergit Bold.ttf</file> <file>Aspergit Italic.ttf</file> <file>Aspergit Bold Italic.ttf</file> </fileset> </family> <!-- Another Font. Can be referenced with ''anotherFont'' or ''bodoni'' --> <family> <nameset> <name>bodoni</name> <name>anotherFont</name> </nameset> <fileset> <file>BodoniFLF-Roman.ttf</file> <file>BodoniFLF-Bold.ttf</file> </fileset> </family> </familyset>

Ahora puede usar estas fuentes en su estilo o xml (siempre que utilice las herramientas que mencioné anteriormente), utilizando el elemento de interfaz de usuario personalizado com.innovattic.font.FontTextView en su diseño xml. A continuación puede ver cómo puede aplicar una fuente a todos los textos en toda su aplicación, simplemente editando res/values/styles.xml :

<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <!-- Application theme --> <!-- Use a different parent if you don''t want Holo Light --> <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> <item name="android:textViewStyle">@style/MyTextViewStyle</item> </style> <!-- Style to use for ALL text views (including FontTextView) --> <!-- Use a different parent if you don''t want Holo Light --> <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView"> <item name="android:textAppearance">@style/MyTextAppearance</item> </style> <!-- Text appearance to use for ALL text views (including FontTextView) --> <!-- Use a different parent if you don''t want Holo Light --> <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo"> <!-- Alternatively, reference this font with the name "aspergit" --> <!-- Note that only our own TextView''s will use the font attribute --> <item name="flFont">someFont</item> <item name="android:textStyle">bold|italic</item> </style> <!-- Alternative style, maybe for some other widget --> <style name="StylishFont"> <item name="flFont">anotherFont</item> <item name="android:textStyle">normal</item> </style> </resources>

Con el res/layout/layout.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <!-- This text view is styled with the app theme --> <com.innovattic.font.FontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This uses my font in bold italic style" /> <!-- This text view is styled here and overrides the app theme --> <com.innovattic.font.FontTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:flFont="anotherFont" android:textStyle="normal" android:text="This uses another font in normal style" /> <!-- This text view is styled with a style and overrides the app theme --> <com.innovattic.font.FontTextView style="@style/StylishFont" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This also uses another font in normal style" /> </LinearLayout>

No te olvides de aplicar el tema en tu manifiesto de Android.


Ejemplo de TextView con tipo de letra roboto:

attr.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RobotoTextView"> <attr name="typeface"/> </declare-styleable> <attr name="typeface" format="enum"> <enum name="roboto_thin" value="0"/> <enum name="roboto_thin_italic" value="1"/> <enum name="roboto_light" value="2"/> <enum name="roboto_light_italic" value="3"/> <enum name="roboto_regular" value="4"/> <enum name="roboto_italic" value="5"/> <enum name="roboto_medium" value="6"/> <enum name="roboto_medium_italic" value="7"/> <enum name="roboto_bold" value="8"/> <enum name="roboto_bold_italic" value="9"/> <enum name="roboto_black" value="10"/> <enum name="roboto_black_italic" value="11"/> <enum name="roboto_condensed" value="12"/> <enum name="roboto_condensed_italic" value="13"/> <enum name="roboto_condensed_bold" value="14"/> <enum name="roboto_condensed_bold_italic" value="15"/> </attr> </resources>

RobotoTextView.java:

public class RobotoTextView extends TextView { /* * Permissible values ​​for the "typeface" attribute. */ private final static int ROBOTO_THIN = 0; private final static int ROBOTO_THIN_ITALIC = 1; private final static int ROBOTO_LIGHT = 2; private final static int ROBOTO_LIGHT_ITALIC = 3; private final static int ROBOTO_REGULAR = 4; private final static int ROBOTO_ITALIC = 5; private final static int ROBOTO_MEDIUM = 6; private final static int ROBOTO_MEDIUM_ITALIC = 7; private final static int ROBOTO_BOLD = 8; private final static int ROBOTO_BOLD_ITALIC = 9; private final static int ROBOTO_BLACK = 10; private final static int ROBOTO_BLACK_ITALIC = 11; private final static int ROBOTO_CONDENSED = 12; private final static int ROBOTO_CONDENSED_ITALIC = 13; private final static int ROBOTO_CONDENSED_BOLD = 14; private final static int ROBOTO_CONDENSED_BOLD_ITALIC = 15; /** * List of created typefaces for later reused. */ private final static SparseArray<Typeface> mTypefaces = new SparseArray<Typeface>(16); /** * Simple constructor to use when creating a view from code. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. */ public RobotoTextView(Context context) { super(context); } /** * Constructor that is called when inflating a view from XML. This is called * when a view is being constructed from an XML file, supplying attributes * that were specified in the XML file. This version uses a default style of * 0, so the only attribute values applied are those in the Context''s Theme * and the given AttributeSet. * <p/> * <p/> * The method onFinishInflate() will be called after all children have been * added. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @see #RobotoTextView(Context, AttributeSet, int) */ public RobotoTextView(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); } /** * Perform inflation from XML and apply a class-specific base style. This * constructor of View allows subclasses to use their own base style when * they are inflating. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. * @param defStyle The default style to apply to this view. If 0, no style * will be applied (beyond what is included in the theme). This may * either be an attribute resource, whose value will be retrieved * from the current theme, or an explicit style resource. * @see #RobotoTextView(Context, AttributeSet) */ public RobotoTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); parseAttributes(context, attrs); } /** * Parse the attributes. * * @param context The Context the view is running in, through which it can access the current theme, resources, etc. * @param attrs The attributes of the XML tag that is inflating the view. */ private void parseAttributes(Context context, AttributeSet attrs) { TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.RobotoTextView); int typefaceValue = values.getInt(R.styleable.RobotoTextView_typeface, 0); values.recycle(); setTypeface(obtaintTypeface(context, typefaceValue)); } /** * Obtain typeface. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto {@link Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface obtaintTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface = mTypefaces.get(typefaceValue); if (typeface == null) { typeface = createTypeface(context, typefaceValue); mTypefaces.put(typefaceValue, typeface); } return typeface; } /** * Create typeface from assets. * * @param context The Context the view is running in, through which it can * access the current theme, resources, etc. * @param typefaceValue values ​​for the "typeface" attribute * @return Roboto {@link Typeface} * @throws IllegalArgumentException if unknown `typeface` attribute value. */ private Typeface createTypeface(Context context, int typefaceValue) throws IllegalArgumentException { Typeface typeface; switch (typefaceValue) { case ROBOTO_THIN: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Thin.ttf"); break; case ROBOTO_THIN_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-ThinItalic.ttf"); break; case ROBOTO_LIGHT: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf"); break; case ROBOTO_LIGHT_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-LightItalic.ttf"); break; case ROBOTO_REGULAR: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular.ttf"); break; case ROBOTO_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Italic.ttf"); break; case ROBOTO_MEDIUM: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); break; case ROBOTO_MEDIUM_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-MediumItalic.ttf"); break; case ROBOTO_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); break; case ROBOTO_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldItalic.ttf"); break; case ROBOTO_BLACK: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Black.ttf"); break; case ROBOTO_BLACK_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BlackItalic.ttf"); break; case ROBOTO_CONDENSED: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Condensed.ttf"); break; case ROBOTO_CONDENSED_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-CondensedItalic.ttf"); break; case ROBOTO_CONDENSED_BOLD: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensed.ttf"); break; case ROBOTO_CONDENSED_BOLD_ITALIC: typeface = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf"); break; default: throw new IllegalArgumentException("Unknown `typeface` attribute value " + typefaceValue); } return typeface; } }

Ejemplo de uso:

<your.package.widget.RobotoTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:typeface="roboto_thin" android:textSize="22sp" android:text="Roboto Thin"/>

Recursos: fuentes Roboto y Noto


El siguiente método, llamado onCreate () y pasado su ViewGroup más externo, funcionará para todo menos para el texto que se crea dinámicamente (es decir, listas dinámicas, alertas, etc.). Una forma fácil de obtener ViewGroup es usar getRootView en cualquiera de sus vistas.

public void onCreate(Bundle savedInstanceState){ //onCreate code... EditText text = (EditText) findViewById(R.id.editText1); setTypeFaceForViewGroup((ViewGroup) text.getRootView()); } private void setTypeFaceForViewGroup(ViewGroup vg){ for (int i = 0; i < vg.getChildCount(); i++) { if (vg.getChildAt(i) instanceof ViewGroup) setTypeFaceForViewGroup((ViewGroup) vg.getChildAt(i)); else if (vg.getChildAt(i) instanceof TextView) ((TextView) vg.getChildAt(i)).setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Your_Font.ttf")); } }

Esto también debería funcionar para contenido dinámico, solo tendrías que llamarlo, pasando lo que creaste, justo después de que lo hayas creado (aunque no lo he probado).

Para ahorrar memoria, probablemente quiera hacer del tipo de letra una variable estática, en lugar de crear una nueva cada vez que el bucle se ejecute como lo hice aquí.


Es muy tarde pero mi ayuda al otro
Creé CustomTextView, que tiene un atributo llamado typeFace, y me preocupa el problema de pérdida de memoria con la carga de tipografía sin almacenamiento en caché.

Primero clase de Fonts que cargan las fuentes de los activos por una sola vez

import android.content.Context; import android.graphics.Typeface; import java.util.Hashtable; /** * Created by tonyhaddad on 7/19/15. */ public class Fonts { private Context context; public Fonts(Context context) { this.context = context; } private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>( 4); public static Typeface getTypeFace(Context context, String fileName) { Typeface tempTypeface = sTypeFaces.get(fileName); if (tempTypeface == null) { String fontPath=null; if(fileName=="metabold") fontPath ="fonts/Meta-Bold.ttf"; else if(fileName=="metanormal") fontPath="fonts/Meta-Normal.ttf"; else if(fileName=="gsligh") fontPath="fonts/gesslight.ttf"; else if(fileName=="bold") fontPath="fonts/Lato-Bold.ttf"; else if(fileName=="rcr") fontPath="fonts/RobotoCondensed-Regular.ttf"; else if(fileName=="mpr") fontPath="fonts/MyriadPro-Regular.otf"; else if(fileName=="rr") fontPath="fonts/Roboto-Regular.ttf"; tempTypeface = Typeface.createFromAsset(context.getAssets(), fontPath); sTypeFaces.put(fileName, tempTypeface); } return tempTypeface; } }

entonces necesitas agregar un atributo personalizado en attrs.xml add this

<declare-styleable name="CustomFontTextView"> <attr name="typeFace" format="string" /> </declare-styleable>

entonces clase personalizada

package package_name; /** * Created by tonyhaddad on 8/26/15. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; import package_name.R; public class CustomFontTextView extends TextView { String typeFace; public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); if (isInEditMode()) { return; } TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.CustomFontTextView, 0, 0); try { typeFace = a.getString(0); } finally { a.recycle(); } if(typeFace!=null && !typeFace.equalsIgnoreCase("")) { Typeface tf = Fonts.getTypeFace(context, typeFace); setTypeface(tf); } init(); } public CustomFontTextView(Context context, AttributeSet attrs) { super(context, attrs); if (isInEditMode()) { return; } TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.CustomFontTextView, 0, 0); try { typeFace = a.getString(0); } finally { a.recycle(); } if(typeFace!=null && !typeFace.equalsIgnoreCase("")) { Typeface tf = Fonts.getTypeFace(context, typeFace); setTypeface(tf); } init(); } public CustomFontTextView(Context context) { super(context); if(typeFace!=null && !typeFace.equalsIgnoreCase("")) { Typeface tf = Fonts.getTypeFace(context, typeFace); setTypeface(tf); } init(); } private void init() { } public String getTypeFace() { return typeFace; } public void setTypeFace(String typeFace) { this.typeFace = typeFace; invalidate(); requestLayout(); } }

y finalmente agregue la vista de texto

<package_name.CustomFontTextView xmlns:custom="http://schemas.android.com/apk/res-auto/package_name" android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="41dp" android:gravity="center_vertical" android:text="text" android:textColor="#000" android:textSize="23sp" custom:typeFace="metanormal"/>

y puedes cambiar la fuente progrmáticamente con el método setTypeFace
también puede mover el espacio de nombres personalizado a su diseño principal si desea usar más de uno de esta vista

Happy Coding :)


Hay una manera bastante fácil de hacerlo a través de XML. Solo necesita crear su propio widget que amplíe TextView.

Primero, crea un archivo en res / values ​​/ attrs.xml con el siguiente contenido:

<resources> <declare-styleable name="TypefacedTextView"> <attr name="typeface" format="string" /> </declare-styleable> </resources>

Después de eso, crea tu widget personalizado:

package your.package.widget; public class TypefacedTextView extends TextView { public TypefacedTextView(Context context, AttributeSet attrs) { super(context, attrs); //Typeface.createFromAsset doesn''t work in the layout editor. Skipping... if (isInEditMode()) { return; } TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView); String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface); styledAttrs.recycle(); if (fontName != null) { Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); setTypeface(typeface); } } }

Como puede ver, el código de arriba leerá una fuente dentro de la carpeta assets /. Para este ejemplo, supongo que hay un archivo llamado "custom.ttf" en la carpeta de activos. Por último, usa el widget en los XML:

<your.package.widget.TypefacedTextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:your_namespace="http://schemas.android.com/apk/res/your.package" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Custom fonts in XML are easy" android:textColor="#FFF" android:textSize="14dip" your_namespace:typeface="custom.ttf" />

Nota: no podrá ver su fuente personalizada en el editor de diseño de Eclipse. Es por eso que puse el isInEditMode() . Pero si ejecuta su aplicación, la fuente personalizada funcionará como un amuleto.

¡Espero eso ayude!


He encontrado información paso a paso en este enlace, enlace: https://github.com/jaydipumaretiya/CustomTypeface/

Hay muchas maneras de usar el tipo de letra correctamente en android, tiene que poner su archivo tipográfico en la carpeta de activos directamente debajo de su main y puede usarlo en tiempo de ejecución.

Otra forma más simple es utilizar la biblioteca predeterminada para establecer el tipo de letra en su archivo xml. Prefiero esta biblioteca tipográfica personalizada para establecer el tipo de letra en TextView, EditText, Button, CheckBox, RadioButton y AutoCompleteTextView y otros wedget en Android.


Me gustaría agregar una nota a la gran solución de leocadiotine . Es perfecto, pero al usar este Custom TextView, muchas veces ralentiza la aplicación, ya que tiene que acceder a los activos cada vez que se crea una vista de texto. Sugiero usar algo como el View Holder pattern en los Adapters , escribí un ejemplo:

public class Fonts { private static final Map<String, Typeface> typefaces = new HashMap<String, Typeface>(); public static Typeface getTypeface(Context ctx, String fontName) { Typeface typeface = typefaces.get(fontName); if (typeface == null) { typeface = Typeface.createFromAsset(ctx.getAssets(), fontName); typefaces.put(fontName, typeface); } return typeface; } }

De esta forma, la aplicación accede a los activos solo una vez por activo, y los mantiene en la memoria para otras necesidades.


No sé si cambia toda la aplicación, pero he logrado cambiar algunos componentes que de otra manera no podrían cambiarse al hacer esto:

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Lucida Sans Unicode.ttf"); Typeface.class.getField("DEFAULT").setAccessible(true); Typeface.class.getField("DEFAULT_BOLD").setAccessible(true); Typeface.class.getField("DEFAULT").set(null, tf); Typeface.class.getField("DEFAULT_BOLD").set(null, tf);


Si está buscando una solución programática más general, creé una clase estática que se puede usar para establecer el tipo de letra de una vista completa (UI de actividad). Tenga en cuenta que estoy trabajando con Mono (C #) pero puede implementarlo fácilmente usando Java.

Puede pasarle a esta clase un diseño o una vista específica que desee personalizar. Si quieres ser súper eficiente, puedes implementarlo usando el patrón Singleton.

public static class AndroidTypefaceUtility { static AndroidTypefaceUtility() { } //Refer to the code block beneath this one, to see how to create a typeface. public static void SetTypefaceOfView(View view, Typeface customTypeface) { if (customTypeface != null && view != null) { try { if (view is TextView) (view as TextView).Typeface = customTypeface; else if (view is Button) (view as Button).Typeface = customTypeface; else if (view is EditText) (view as EditText).Typeface = customTypeface; else if (view is ViewGroup) SetTypefaceOfViewGroup((view as ViewGroup), customTypeface); else Console.Error.WriteLine("AndroidTypefaceUtility: {0} is type of {1} and does not have a typeface property", view.Id, typeof(View)); } catch (Exception ex) { Console.Error.WriteLine("AndroidTypefaceUtility threw:/n{0}/n{1}", ex.GetType(), ex.StackTrace); throw ex; } } else { Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / view parameter should not be null"); } } public static void SetTypefaceOfViewGroup(ViewGroup layout, Typeface customTypeface) { if (customTypeface != null && layout != null) { for (int i = 0; i < layout.ChildCount; i++) { SetTypefaceOfView(layout.GetChildAt(i), customTypeface); } } else { Console.Error.WriteLine("AndroidTypefaceUtility: customTypeface / layout parameter should not be null"); } } }

En su actividad, deberá crear un objeto Typeface. Creo el mío en OnCreate () usando un archivo .ttf colocado en mi directorio de Recursos / Activos. Asegúrese de que el archivo esté marcado como un elemento de Android en sus ''propiedades.

protected override void OnCreate(Bundle bundle) { ... LinearLayout rootLayout = (LinearLayout)FindViewById<LinearLayout>(Resource.Id.signInView_LinearLayout); Typeface allerTypeface = Typeface.CreateFromAsset(base.Assets,"Aller_Rg.ttf"); AndroidTypefaceUtility.SetTypefaceOfViewGroup(rootLayout, allerTypeface); }


EDITAR : ¡Así que ha pasado un tiempo y me gustaría agregar lo que creo que es la mejor manera de hacerlo, y a través de XML nada menos!

Entonces, primero querrás crear una nueva clase que anule la Vista que quieras personalizar. (por ejemplo, ¿quieres un botón con un tipo de letra personalizado? Ampliar Button ). Hagamos un ejemplo:

public class CustomButton extends Button { private final static int ROBOTO = 0; private final static int ROBOTO_CONDENSED = 1; public CustomButton(Context context) { super(context); } public CustomButton(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); //I''ll explain this method later } public CustomButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); parseAttributes(context, attrs); } }

Ahora, si no tiene uno, agregue un documento XML en res/values/attrs.xml y agregue:

<resources> <!-- Define the values for the attribute --> <attr name="typeface" format="enum"> <enum name="roboto" value="0"/> <enum name="robotoCondensed" value="1"/> </attr> <!-- Tell Android that the class "CustomButton" can be styled, and which attributes it supports --> <declare-styleable name="CustomButton"> <attr name="typeface"/> </declare-styleable> </resources>

Bueno, con eso fuera del camino, volvamos al método parseAttributes() de antes:

private void parseAttributes(Context context, AttributeSet attrs) { TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomButton); //The value 0 is a default, but shouldn''t ever be used since the attr is an enum int typeface = values.getInt(R.styleable.CustomButton_typeface, 0); switch(typeface) { case ROBOTO: default: //You can instantiate your typeface anywhere, I would suggest as a //singleton somewhere to avoid unnecessary copies setTypeface(roboto); break; case ROBOTO_CONDENSED: setTypeface(robotoCondensed); break; } values.recycle(); }

Ahora estás listo. Puede agregar más atributos para todo (puede agregar otro para tipo de letra tipográfico - negrita, cursiva, etc.) pero ahora veamos cómo usarlo:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.yourpackage.name" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.yourpackage.name.CustomButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" custom:typeface="roboto" /> </LinearLayout>

La línea xmlns:custom realmente puede ser cualquier cosa, pero la convención es lo que se muestra arriba. Lo que importa es que es único, y es por eso que se usa el nombre del paquete. Ahora solo usa el prefijo custom: para sus atributos, y el prefijo android: para los atributos de Android.

Una última cosa: si desea usar esto en un estilo ( res/values/styles.xml ), no debe agregar xmlns:custom línea xmlns:custom . Simplemente haga referencia al nombre del atributo sin prefijo:

<style name="MyStyle> <item name="typeface">roboto</item> </style>

(PREVIOUS ANSWER)

Usar una tipografía personalizada en Android

Esto debería ayudar. Básicamente, no hay forma de hacer esto en XML, y hasta donde puedo decir, no hay una manera más fácil de hacerlo en código. Siempre puede tener un método setLayoutFont () que crea el tipo de letra una vez, luego ejecuta setTypeface () para cada uno. Tendría que actualizarlo cada vez que agregue un nuevo elemento a un diseño. Algo como a continuación:

public void setLayoutFont() { Typeface tf = Typeface.createFromAsset( getBaseContext().getAssets(), "fonts/BPreplay.otf"); TextView tv1 = (TextView)findViewById(R.id.tv1); tv1.setTypeface(tf); TextView tv2 = (TextView)findViewById(R.id.tv2); tv2.setTypeface(tf); TextView tv3 = (TextView)findViewById(R.id.tv3); tv3.setTypeface(tf); }

EDITAR : Así que simplemente llegué a implementar algo como esto yo mismo, y cómo terminé haciéndolo fue hacer una función como esta:

public static void setLayoutFont(Typeface tf, TextView...params) { for (TextView tv : params) { tv.setTypeface(tf); } }

Luego, solo use este método desde onCreate (), y pase todas las TextViews que quiera actualizar:

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf"); //find views by id... setLayoutFont(tf, tv1, tv2, tv3, tv4, tv5);

EDITAR 5/9/12:

Entonces, dado que esto sigue recibiendo visitas y votos, me gustaría agregar un método mucho mejor y más completo:

Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/BPreplay.otf"); ViewGroup root = (ViewGroup)findViewById(R.id.myrootlayout); setFont(root, mFont); /* * Sets the font on all TextViews in the ViewGroup. Searches * recursively for all inner ViewGroups as well. Just add a * check for any other views you want to set as well (EditText, * etc.) */ public void setFont(ViewGroup group, Typeface font) { int count = group.getChildCount(); View v; for(int i = 0; i < count; i++) { v = group.getChildAt(i); if(v instanceof TextView || v instanceof Button /*etc.*/) ((TextView)v).setTypeface(font); else if(v instanceof ViewGroup) setFont((ViewGroup)v, font); } }

Si le pasa la raíz de su diseño, buscará de forma recursiva las vistas TextView o Button (o cualquier otra que agregue a esa declaración if) dentro de ese diseño, y establecerá la fuente sin tener que especificarlas por ID. Por supuesto, esto supone que desea establecer la fuente para cada vista.