836 Stimmen

Wie ändert man die fontFamily von TextView in Android

Ich möchte also die android:fontFamily in Android, aber ich sehe keine vordefinierten Schriftarten in Android. Wie kann ich eine der vordefinierten Schriftarten auswählen? Ich brauche nicht wirklich meine eigene TypeFace zu definieren, aber alles, was ich brauche, ist etwas anderes als das, was es jetzt zeigt.

<TextView
    android:id="@+id/HeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="52dp"
    android:gravity="center"
    android:text="CallerBlocker"
    android:textSize="40dp"
    android:fontFamily="Arial"
 />

Es scheint, dass das, was ich da oben gemacht habe, nicht wirklich funktioniert! ÜBRIGENS: android:fontFamily="Arial" war ein dummer Versuch!

0voto

Sanny Nagveker Punkte 418

Sie können dies auf einfache Weise mit der folgenden Bibliothek tun

https://github.com/sunnag7/FontStyler

<com.sunnag.fontstyler.FontStylerView
              android:textStyle="bold"
              android:text="@string/about_us"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="8dp"
              app:fontName="Lato-Bold"
              android:textSize="18sp"
              android:id="@+id/textView64" />

Es ist leicht und einfach zu implementieren, kopieren Sie einfach Ihre Schriften in den Asset-Ordner und verwenden Sie den Namen in der XML-Datei.

0voto

Setmax Punkte 893

Ich benutze Briefpresse lib für mein NonTextView-Zeug wie Buttons und kianoni fontloader lib für meine TextViews, weil die Verwendung von Stilen in dieser Lib für mich einfacher ist als Letter Press und ich damit ideales Feedback bekommen habe. Dies ist großartig für diejenigen, die eine benutzerdefinierte Schriftart außer Roboto Font verwenden möchten. Das war also meine Erfahrung mit Schriftarten-Libs. Für diejenigen, die eine benutzerdefinierte Klasse verwenden möchten, um die Schriftart zu ändern, empfehle ich dringend, diese Klasse mit diesem Snippet zu erstellen

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
            new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link android.graphics.Typeface} and apply to a {@link android.text.Spannable}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                    .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
} 

Und verwenden Sie die Klasse wie folgt:

AppData = PreferenceManager.getDefaultSharedPreferences(this);
TextView bannertv= (TextView) findViewById(R.id.txtBanner);
    SpannableString s = new SpannableString(getResources().getString(R.string.enterkey));
    s.setSpan(new TypefaceSpan(this, AppData.getString("font-Bold",null)), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    bannertv.setText(s);

vielleicht hilft das.

0voto

Gnod Punkte 147

Aquí können Sie alle verfügbaren Schriftfamilienwerte und die entsprechenden Schriftdateinamen sehen (diese Datei wird in Android 5.0+ verwendet). Auf einem mobilen Gerät finden Sie sie in:

/system/etc/fonts.xml (for 5.0+)

(Für Android 4.4 und niedriger mit diese Version, aber ich denke, dass fonts.xml hat ein klareres Format und ist leicht zu verstehen).

Zum Beispiel,

    <!-- first font is default -->
20    <family name="sans-serif">
21        <font weight="100" style="normal">Roboto-Thin.ttf</font>
22        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
23        <font weight="300" style="normal">Roboto-Light.ttf</font>
24        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
25        <font weight="400" style="normal">Roboto-Regular.ttf</font>
26        <font weight="400" style="italic">Roboto-Italic.ttf</font>
27        <font weight="500" style="normal">Roboto-Medium.ttf</font>
28        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
29        <font weight="900" style="normal">Roboto-Black.ttf</font>
30        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
31        <font weight="700" style="normal">Roboto-Bold.ttf</font>
32        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
33    </family>

Das Attribut name name="sans-serif" von family Tag den Wert definiert, den Sie in Android:fontFamily verwenden können.

En font Tag definiert die zugehörigen Schriftartendateien.

In diesem Fall können Sie die Quelle unter <!-- fallback fonts --> Sie wird für die Fallback-Logik der Schriftarten verwendet.

0voto

Bholendra Singh Punkte 747
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
    <font
        android:font="@font/google_sans_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/google_sans_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/google_sans_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />

    <font
        android:font="@font/google_sans_medium"
        android:fontStyle="normal"
        android:fontWeight="500"
        app:font="@font/google_sans_medium"
        app:fontStyle="normal"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_medium_italic"
        android:fontStyle="italic"
        android:fontWeight="500"
        app:font="@font/google_sans_medium_italic"
        app:fontStyle="italic"
        app:fontWeight="500" />

    <font
        android:font="@font/google_sans_bold"
        android:fontStyle="normal"
        android:fontWeight="600"
        app:font="@font/google_sans_bold"
        app:fontStyle="normal"
        app:fontWeight="600" />

    <font
        android:font="@font/google_sans_bold_italic"
        android:fontStyle="italic"
        android:fontWeight="600"
        app:font="@font/google_sans_bold_italic"
        app:fontStyle="italic"
        app:fontWeight="600" />

</font-family>

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X