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!

3voto

EpicPandaForce Punkte 75417

Sie können eine benutzerdefinierte FontFamily wie folgt definieren:

/res/font/usual.xml :

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

    <font
        android:fontStyle="italic"
        android:fontWeight="200"
        android:font="@font/usual_regular_italic"
        app:fontStyle="italic"
        app:fontWeight="200"
        app:font="@font/usual_regular_italic" />

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

    <font
        android:fontStyle="italic"
        android:fontWeight="600"
        android:font="@font/usual_bold_italic"
        app:fontStyle="italic"
        app:fontWeight="600"
        app:font="@font/usual_bold_italic" />
</font-family>

Jetzt können Sie

android:fontFamily="@font/usual"

Angenommen, Ihre anderen font Ressourcen sind ebenfalls vorhanden, mit Kleinbuchstaben und _ s.

2voto

Stoycho Andreev Punkte 5914

Ich möchte nur erwähnen, dass die Hölle mit den Schriftarten in Android bald ein Ende hat, denn dieses Jahr auf der Google IO haben wir endlich dieses -> https://developer.Android.com/preview/features/working-with-fonts.html

Jetzt gibt es einen neuen Ressourcentyp a Schriftart und Sie können alle Ihre Anwendungsschriften im Ordner res/fonts ablegen und dann mit R.font.my_custom_font darauf zugreifen, genau wie Sie auf String res-Werte, auslosbar res-Werte usw. Sie haben sogar die Möglichkeit zu erstellen font-face xml-Datei, die Ihre benutzerdefinierten Schriftarten (über kursiv, fett und unterstrichen attr) setzen wird.

Lesen Sie den obigen Link für weitere Informationen. Lassen Sie uns die Unterstützung sehen.

2voto

Aziz Punkte 1628

Die neue Schriftart-Ressource ermöglicht das direkte Setzen von font mit

android:fontFamily="@font/my_font_in_font_folder"

2voto

Ali Khaki Punkte 1150

Für Android-Studio 3 und höher können Sie diesen Stil verwenden und dann alle textView Schriftartänderungen in der Anwendung.

erstellen Sie diesen Stil in Ihrem style.xml :

<!--OverRide all textView font-->
<style name="defaultTextViewStyle" parent="android:Widget.TextView">
        <item name="android:fontFamily">@font/your_custom_font</item>
</style>

Verwenden Sie es dann in Ihrem Thema:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textViewStyle">@style/defaultTextViewStyle</item>
</style>

2voto

CoolMind Punkte 21914

Sie können die Standardschriftarten auch mit setTextAppearance (erfordert API 16), siehe https://stackoverflow.com/a/36301508/2914140 :

<style name="styleA">
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="styleB">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?android:attr/textColorTertiary</item>
</style>

if(condition){
    TextViewCompat.setTextAppearance(textView, R.style.styleA);
} else {
    TextViewCompat.setTextAppearance(textView,R.style.styleB);
}

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