How to change fontFamily of TextView in Android

This article was translated from: How to change fontFamily of TextView in Android

So I'd like to change the android:fontFamilyin Android but I don't see any pre-defined fonts in Android. So I want android:fontFamily to change in Android, but I don't see any predefined fonts in Android . How do I select one of the pre-defined ones? How do I select one of the pre-defined ones ? I don't really need to define my own TypeFace but all I need is something different from what it shows right now .

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

It seems what I did up there won't really work! It seems that what I did up there doesn't really work! BTW android:fontFamily="Arial"was a stupid attempt! By the way, it was a stupid attempt !android:fontFamily="Arial"


#1st Floor

Reference: https://stackoom.com/question/ot8F/ How to change the fontFamily of TextView in Android


#2nd Floor

What you want is not possible. What you want is not possible . The MUST need to the SET by You TypeFacein your Code. You must set in code TypeFace.

An In XMLthe What you CAN do IS in XML, you can do that

android:typeface="sans" | "serif" | "monospace"

other then this you can not play much with the Fonts in XML. Otherwise, you will not be able to use fonts in XML. :) :)

The For Arialyou need to the SET type face in your code. For the Arialneed to set the type face in your code.


#3rd floor

This is the way to set the font programmatically: This is the way to set the font programmatically :

TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/epimodem.ttf");
tv.setTypeface(face);

Put the font file in your assets folder. Put the font file in your assets folder . In my case I created a subdirectory called fonts. In my case, I created a subdirectory called fonts .

EDIT: If you wonder where is your assets folder see this question EDIT: If you want to know where your assets folder is, please refer to this question


#4th floor

At The Same, AS's IT android:typeface. And android:typefacethe same.

built-in fonts are: built-in fonts are :

  • normal normal
  • sans no
  • serif Serif
  • monospace width

See android:typeface . 参见android:typeface


#5th Floor

Android doesn't allow you to set custom fonts from the XML layout. Android doesn't allow you to set custom fonts from the XML layout . Instead, you must bundle the specific font file in your app's assets folder, and set it programmatically. Instead, you must bundle the specific font file in the app's asset folder and set it programmatically . Something like: like:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
textView.setTypeface(typeFace);

Note that you can only run this code after setContentView () has been called. Please note that you can only run this code after calling setContentView (). Also, only some fonts are supported by Android, and should be in a .ttf (TrueType)or .otf (OpenType)format. In addition, Android only supports certain fonts, and should use .ttf (TrueType)or .otf (OpenType)format. Even then, some fonts may not work. Even then, some fonts may not work .

The this IS A font that definitely Works ON Android, and you CAN use the this that your code to Confirm The IS Working in Case File your font IS not Supported by Android. This is an absolute that can be used on Android font, if not Android Support your font file, you can use it to confirm that your code can work normally.

Android O Update: This is now possible with XML in Android O , based on Roger's comment. Android O Update: Based on Roger's comment, you can now use XML in Android O to update.


#6th floor

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available: Starting from android 4.1 / 4.2 / 5.0, the following Roboto font family can be used :

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black"     // roboto black
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

Enter picture description here

in combination with in combination with ...

android:textStyle="normal|bold|italic"

this 16 variants are possible: these 16 variants are possible :

  • Roboto regular robot routine
  • Roboto italic italic
  • Roboto bold Roboto bold
  • Roboto bold italic Roboto Coarse italic
  • Roboto-Light robot light
  • Roboto-Light italic Roboto-Light斜体
  • Roboto-Thin mechanical Thin
  • Roboto-Thin italic Roboto-Thin斜体
  • Roboto-Condensed concentrated robot
  • Roboto-Condensed italic Roboto compressed italic
  • Roboto-Condensed bold robot concentrated blackbody
  • Roboto-Condensed bold italic Roboto-compressed bold italic
  • Roboto-Black Robot Black
  • Roboto-Black italic Roboto-Black italic
  • Roboto-Medium Robot
  • Roboto-Medium italic mechanical italics

fonts.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="font_family_light">sans-serif-light</string>
    <string name="font_family_medium">sans-serif-medium</string>
    <string name="font_family_regular">sans-serif</string>
    <string name="font_family_condensed">sans-serif-condensed</string>
    <string name="font_family_black">sans-serif-black</string>
    <string name="font_family_thin">sans-serif-thin</string>
</resources>
Published 0 original articles · praised 75 · 560,000 views +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105472246