如何在Android中更改TextView的fontFamily

本文翻譯自:How to change fontFamily of TextView in Android

So I'd like to change the android:fontFamily in Android but I don't see any pre-defined fonts in Android. 所以我想在Android中更改android:fontFamily ,但在Android中看不到任何預定義的字體。 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. 我實際上不需要定義自己的TypeFace,但我所需要的只是與現在顯示的內容有所不同。

<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! 看來我在那兒所做的並不能真正起作用! BTW android:fontFamily="Arial" was a stupid attempt! 順便說一句android:fontFamily="Arial"是一個愚蠢的嘗試!


#1樓

參考:https://stackoom.com/question/ot8F/如何在Android中更改TextView的fontFamily


#2樓

What you want is not possible. 您想要的是不可能的。 You must need to set TypeFace in your Code. 您必須在代碼中設置TypeFace

In XML what you can do is XML ,您可以做的是

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

other then this you can not play much with the Fonts in XML. 否則,您將無法在XML中使用字體。 :) :)

For Arial you need to set type face in your code. 對於Arial您需要在代碼中設置type face。


#3樓

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. 將字體文件放在資產文件夾中。 In my case I created a subdirectory called fonts. 就我而言,我創建了一個名爲fonts的子目錄。

EDIT: If you wonder where is your assets folder see this question 編輯:如果您想知道您的資產文件夾在哪裏,請參閱此問題


#4樓

It's the same as android:typeface . android:typeface相同。

built-in fonts are: 內置字體有:

  • normal 正常
  • sans
  • serif 襯線
  • monospace 等寬

See android:typeface . 參見android:typeface


#5樓

Android doesn't allow you to set custom fonts from the XML layout. Android不允許您從XML佈局中設置自定義字體。 Instead, you must bundle the specific font file in your app's assets folder, and set it programmatically. 相反,您必須將特定字體文件捆綁在應用程序的資產文件夾中,並以編程方式進行設置。 Something 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. 請注意,只有在調用setContentView()之後才能運行此代碼。 Also, only some fonts are supported by Android, and should be in a .ttf (TrueType) or .otf (OpenType) format. 另外,Android僅支持某些字體,並且應採用.ttf (TrueType).otf (OpenType)格式。 Even then, some fonts may not work. 即使那樣,某些字體也可能不起作用。

This is a font that definitely works on Android, and you can use this to confirm that your code is working in case your font file isn't supported by Android. 是一種絕對可以在Android上使用的字體,如果Android不支持您的字體文件,您可以使用它來確認您的代碼可以正常工作。

Android O Update: This is now possible with XML in Android O , based on Roger's comment. Android O更新:基於Roger的評論,現在可以使用Android O中的XML進行更新。


#6樓

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available: 從android 4.1 / 4.2 / 5.0起,可以使用以下Roboto字體系列:

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)

在此處輸入圖片說明

in combination with 與...結合

android:textStyle="normal|bold|italic"

this 16 variants are possible: 這16種變體是可能的:

  • Roboto regular 機械手常規
  • Roboto italic 斜體
  • Roboto bold Roboto粗體
  • Roboto bold italic Roboto粗體斜體
  • Roboto-Light 機器人光
  • Roboto-Light italic Roboto-Light斜體
  • Roboto-Thin 機械薄型
  • Roboto-Thin italic Roboto-Thin斜體
  • Roboto-Condensed 濃縮的機器人
  • Roboto-Condensed italic Roboto壓縮斜體
  • Roboto-Condensed bold 機械手濃縮黑體
  • Roboto-Condensed bold italic Roboto壓縮的粗體斜體
  • Roboto-Black 機器人黑
  • Roboto-Black italic Roboto-Black斜體
  • Roboto-Medium 機械人
  • Roboto-Medium italic 機械斜體

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章