How to change fontFamily of TextView in Android

后端 未结 30 2408
感动是毒
感动是毒 2020-11-22 01:05

So I\'d like to change the android:fontFamily in Android but I don\'t see any pre-defined fonts in Android. How do I select one of the pre-defined ones? I don\'

相关标签:
30条回答
  • 2020-11-22 01:45

    Here is an easier way that can work in some cases. The principle is to add a not visible TextVview in your xml layout and to get its typeFace in the java code.

    The layout in the xml file:

     <TextView
            android:text="The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty. The classic bread is made of flour hot and salty."
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:fontFamily="sans-serif-thin"
            android:id="@+id/textViewDescription"/>
    

    And the java code:

    myText.setTypeface(textViewSelectedDescription.getTypeface());
    

    It has worked for me (within a TextSwitcher for example).

    0 讨论(0)
  • 2020-11-22 01:47

    Starting from Android-Studio 3.0 its very easy to change font family

    Using support library 26, it will work on devices running Android API version 16 and higher

    Create a folder font under res directory .Download the font which ever you want and paste it inside font folder. The structure should be some thing like below

    Note: As of Android Support Library 26.0, you must declare both sets of attributes ( android: and app: ) to ensure your fonts load on devices running Api 26 or lower.

    Now you can change font in layout using

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/dancing_script"
    app:fontFamily="@font/dancing_script"/>
    

    To change Programatically

     Typeface typeface = getResources().getFont(R.font.myfont);
       //or to support all versions use
    Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
     textView.setTypeface(typeface);  
    

    To change font using styles.xml create a style

     <style name="Regular">
            <item name="android:fontFamily">@font/dancing_script</item>
            <item name="fontFamily">@font/dancing_script</item>
            <item name="android:textStyle">normal</item>
     </style>
    

    and apply this style to TextView

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Regular"/>
    

    you can also Create your own font family

    - Right-click the font folder and go to New > Font resource file. The New Resource File window appears.

    - Enter the file name, and then click OK. The new font resource XML opens in the editor.

    Write your own font family here , for example

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

    this is simply a mapping of a specific fontStyle and fontWeight to the font resource which will be used to render that specific variant. Valid values for fontStyle are normal or italic; and fontWeight conforms to the CSS font-weight specification

    1. To change fontfamily in layout you can write

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>
    

    2. To Change Programmatically

     Typeface typeface = getResources().getFont(R.font.lobster);
       //or to support all versions use
    Typeface typeface = ResourcesCompat.getFont(context, R.font.lobster);
     textView.setTypeface(typeface);  
    

    To change font of entire App Add these two lines in AppTheme

     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
         <item name="android:fontFamily">@font/your_font</item>
         <item name="fontFamily">@font/your_font</item>
      </style>
    

    See the Documentation , Android Custom Fonts Tutorial For more info

    0 讨论(0)
  • 2020-11-22 01:47

    The new font resource allows to directly set font using

    android:fontFamily="@font/my_font_in_font_folder"
    
    0 讨论(0)
  • 2020-11-22 01:50

    To set Roboto programmatically:

    paint.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));
    
    0 讨论(0)
  • 2020-11-22 01:50

    An easy way to manage the fonts would be to declare them via resources, as such:

    <!--++++++++++++++++++++++++++-->
    <!--added on API 16 (JB - 4.1)-->
    <!--++++++++++++++++++++++++++-->
    <!--the default font-->
    <string name="fontFamily__roboto_regular">sans-serif</string>
    <string name="fontFamily__roboto_light">sans-serif-light</string>
    <string name="fontFamily__roboto_condensed">sans-serif-condensed</string>
    
    <!--+++++++++++++++++++++++++++++-->
    <!--added on API 17 (JBMR1 - 4.2)-->
    <!--+++++++++++++++++++++++++++++-->
    <string name="fontFamily__roboto_thin">sans-serif-thin</string>
    
    <!--+++++++++++++++++++++++++++-->
    <!--added on Lollipop (LL- 5.0)-->
    <!--+++++++++++++++++++++++++++-->
    <string name="fontFamily__roboto_medium">sans-serif-medium</string>
    <string name="fontFamily__roboto_black">sans-serif-black</string>
    <string name="fontFamily__roboto_condensed_light">sans-serif-condensed-light</string>
    

    This is based on the source code here and here

    0 讨论(0)
  • 2020-11-22 01:52

    You can also do this by adding a font folder under the res directory like below.

    Then, selecting Font as the resource type.

    You can find avaliable fonts from https://www.1001fonts.com/, and then extracting the TTF files to this font directory.

    Finally, just change the XML file that contains your textview by adding android:fontFamily:"@font/urfontfilename"

    0 讨论(0)
提交回复
热议问题