1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 如何在Android中更改TextView的fontFamily

如何在Android中更改TextView的fontFamily

时间:2021-07-22 04:15:09

相关推荐

如何在Android中更改TextView的fontFamily

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

So I'd like to change theandroid:fontFamilyin 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,但我所需要的只是与现在显示的内容有所不同。

<TextViewandroid: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!看来我在那儿所做的并不能真正起作用!BTWandroid:fontFamily="Arial"was a stupid attempt!顺便说一句android:fontFamily="Arial"是一个愚蠢的尝试!

#1楼

参考:/question/ot8F/如何在Android中更改TextView的fontFamily

#2楼

What you want is not possible.您想要的是不可能的。You must need to setTypeFacein your Code.您必须在代码中设置TypeFace

InXMLwhat you can do is在XML,您可以做的是

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

other then this you can not play much with the Fonts in XML.否则,您将无法在XML中使用字体。:):)

ForArialyou 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 asandroid: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 regularandroid:fontFamily="sans-serif-light"// roboto lightandroid:fontFamily="sans-serif-condensed" // roboto condensedandroid:fontFamily="sans-serif-black"// roboto blackandroid: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 boldRoboto粗体Roboto bold italicRoboto粗体斜体Roboto-Light机器人光Roboto-Light italicRoboto-Light斜体Roboto-Thin机械薄型Roboto-Thin italicRoboto-Thin斜体Roboto-Condensed浓缩的机器人Roboto-Condensed italicRoboto压缩斜体Roboto-Condensed bold机械手浓缩黑体Roboto-Condensed bold italicRoboto压缩的粗体斜体Roboto-Black机器人黑Roboto-Black italicRoboto-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>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。