Desafortunadamente, Android no proporciona la forma más rápida, fácil y limpia que busca cambiar la fuente para toda la 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 leer todo al respecto en this blog, y ver el proyecto github here.
Aquí hay un ejemplo de cómo aplicar estas herramientas. Coloque todos sus 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);
(por ejemplo, en la clase onCreate of your Application). El archivo XML es el siguiente:
<?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 utilizar estas fuentes en su estilo o XML (siempre y cuando use las herramientas que he mencionado anteriormente), estableciendo el atributo flFont en el TextView encargo com.innovattic.font.FontTextView
en su diseño xml . A continuación se puede ver cómo se 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 acompañamiento 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 se olvide de aplicar el tema en tu manifiesto de Android.
sí, pero no lo que está pidiendo – user1010160