Android - Using Custom Font

后端 未结 21 1663
别跟我提以往
别跟我提以往 2020-11-22 04:38

I applied a custom font to a TextView, but it doesn\'t seems to change the typeface.

Here is my code:

    Typeface myTypeface = Typeface         


        
相关标签:
21条回答
  • 2020-11-22 05:08

    Make sure to paste the above code into onCreate() after your call to the super and the call to setContentView(). This small detail kept my hung up for awhile.

    0 讨论(0)
  • 2020-11-22 05:10

    Well, after seven years you can change whole app textView or what you want easily by using android.support libraries 26++.

    E.g:

    Create your font package app/src/res/font and move your font into it.

    And in your app theme just add it as a fontFamily:

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

    Example for use with textView only:

    <style name="fontTextView" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">monospace</item>
    </style>
    

    And add into your main theme:

    <item name="android:textViewStyle">@style/fontTextView</item>
    

    Currently it's worked on 8.1 until 4.1 API Jelly Bean And that's a wide range.

    0 讨论(0)
  • 2020-11-22 05:11

    If you want to load the font from the network or easily style it, you can use:

    https://github.com/shellum/fontView

    Example:

    <!--Layout-->
    <com.finalhack.fontview.FontView
            android:id="@+id/someFontIcon"
            android:layout_width="80dp"
            android:layout_height="80dp" />
    
    //Java:
    fontView.setupFont("http://blah.com/myfont.ttf", true, character, FontView.ImageType.CIRCLE);
    fontView.addForegroundColor(Color.RED);
    fontView.addBackgroundColor(Color.WHITE);
    
    0 讨论(0)
  • 2020-11-22 05:12
    • Open your project and select Project on the top left
    • app --> src --> main
    • right click to main and create directory name it as assets
    • right click to assest and create new directory name it fonts
    • you need to find free fonts like free fonts
    • give it to your Textview and call it in your Activity class
    • copy your fonts inside the fonts folder
    • TextView txt = (TextView) findViewById(R.id.txt_act_spalsh_welcome); Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Aramis Italic.ttf"); txt.setTypeface(font);

    name of the font must be correct and have fun

    0 讨论(0)
  • 2020-11-22 05:13

    After trying most of the solutions described in this thread, I accidentally found Calligraphy (https://github.com/chrisjenx/Calligraphy) - a library by Christopher Jenkins that lets you easily add custom fonts to your app. The advantages of his lib comparing to approaches suggested here are:

    1. you don't have to introduce your own overriden TextView component, you use the built-in TextView
    2. you can easily include the library using gradle
    3. The library doesn't limit your choice of fonts; you just add your preferred ones to the assets dir
    4. you not only get custom text views — all the other text-based Android compontents will also be displayed using your custom font.
    0 讨论(0)
  • 2020-11-22 05:13

    With Android 8.0 using Custom Fonts in Application became easy with downloadable fonts. We can add fonts directly to the res/font/ folder in the project folder, and in doing so, the fonts become automatically available in Android Studio.

    Now set fontFamily attribute to list of fonts or click on more and select font of your choice. This will add tools:fontFamily="@font/your_font_file" line to your TextView.

    This will Automatically generate few files.

    1. In values folder it will create fonts_certs.xml.

    2. In Manifest it will add this lines:

      <meta-data
                android:name="preloaded_fonts"
                android:resource="@array/preloaded_fonts" /> 
    

    3. preloaded_fonts.xml

    <resources>
        <array name="preloaded_fonts" translatable="false">
            <item>@font/open_sans_regular</item>
            <item>@font/open_sans_semibold</item>
        </array>
    </resources>
    
    0 讨论(0)
提交回复
热议问题