Change the font of all text views

Is there a way to change the fonts of all text views in the layout?

im is currently used to change fonts manually.

TextView txtAppName = (TextView) findViewById(R.id.txtAppName);

Typeface tf = Typeface.createFromAsset(getAssets(),
    "fonts/Existence-Light.otf");
txtAppName.setTypeface(tf);

EDIT: I edited the question to clarify my problem.

+5
source share
5 answers

You may be a little late, but for other Android users this can be very useful.

, Android , , . , - ( xml, ). github .

, . assets/fonts/. xml TypefaceManager.initialize(this, R.xml.fonts); (, onCreate Application). xml :

<?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>

xml flFont. , :

<?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 will use the font attribute -->
        <item name="flFont">someFont</item>
        <item name="android:textStyle">bold|italic</item>
    </style>

</resources>

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" >

    <com.innovattic.font.FontTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This uses my font in bold italic style" />

    <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" />

</LinearLayout>

Android.

+6

,

public class MyTextView extends TextView {
     public MyTextView(Context context) {
        super(context, attrs, defStyle);
        setFont(context);
     }

     private void setFont(Context context) {
        Typeface font = Typeface.createFromAsset(context.getAssets(), YOUR_FONT);
        setTypeface(font);
     }
}
+3

, , , .

TextView .

+2

, assests- > fonts, : enter image description here

"ttf".

TextViews (my is "fonts/Qlassik_TB.ttf" ) :

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Your stuff here...

    Typeface tf = Typeface.createFromAsset(this.getAssets(), "fonts/Qlassik_TB.ttf");

    ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);

    setFontToAllChilds(myMostParentLayout, tf);
}

private void setFontToAllChilds(ViewGroup myMostParentLayout, Typeface tf) {
    int childCount = myMostParentLayout.getChildCount();
    for (int i = 0; i < childCount; ++i) {
        View child = myMostParentLayout.getChildAt(i);

        if (child instanceof ViewGroup)
            setFontToAllChilds((ViewGroup) child, tf);
        else if (child instanceof TextView)                
            ((TextView) child).setTypeface(tf);
}

, , . WhateverLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_layout_most_parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

:

ViewGroup myMostParentLayout = (ViewGroup) findViewById(R.id.frame_layout_most_parent);
+1

, , :

/RES//styles.xml

, . , , . , .

0

All Articles