在API <26中使用XML中的字体

szi*_*qui 20 fonts android

https://developer.android.com/preview/features/working-with-fonts.html

Android API 26支持这一点但是我们是否有一个支持库可以将字体用作res中的资源?[font in XML]如果是,那么它支持哪种API?

我在谈论Android O SDK功能,它允许我们在上面的链接中给出XML格式.这不是MM中的原生功能我敢肯定.

小智 31

根据谷歌的说法,只要满足以下条件,Android 4.0+(API 14+)就支持它:

  • 您正在使用支持库版本26.0.0-beta1或更高版本
  • 您正在使用AppCompat

资料来源:

XML中的字体 - 使用支持库

支持库修订历史记录

我希望我可以在早于8(Android 26)的Android版本的app小部件中使用它,但是这是不可能的,因为AppCompatTextView不能在app小部件中使用.Android O的"字体在XML中"的第三方替代品(例如Caligraphy库)也没有在app小部件中工作.

app小部件的唯一替代方法是使用ImageView而不是TextView,并使用诸如setImageViewUri(...)setImageViewBitmap(...)之类的RemoteViews方法,这两种方法都存在问题.

  • 感谢您的指导。我将&lt;TextView&gt;更改为&lt;android.support.v7.widget.AppCompatTextView&gt;,现在可以使用了。 (2认同)

Yur*_*yac 25

要支持API低于26的字体系列,我们需要使用

<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>
Run Code Online (Sandbox Code Playgroud)

代替

<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"/>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。这应该被标记为正确答案。 (3认同)

Jem*_*rov 16

为了完成Yuri的回答,Font支持API16。要创建字体系列(在其中指定了不同的字体粗细),应在doc中编写的/ res / font /my_font_family.xml下创建:

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont_regular"/>
    <font app:fontStyle="normal" app:fontWeight="600" app:font="@font/myfont_semi_bold" />
    <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont_italic" />
</font-family>
Run Code Online (Sandbox Code Playgroud)

要使用这个TextViewXML,你应该使用app:fontFamily="@font/my_font_family",小心app:代替android:

要选择specific fontWeight,可以使用,android:textFontWeight="200"但仅适用于API> = 28。要根据API 28之前的权重选择不同的字体,您有两个选择。您可以 android:textStyle="bold"只指定“正常/斜体/粗体”,而不能指定确切的重量。或者,您可以像这样直接使用特定的字体粗细: app:fontFamily="@font/myfont_semi_bold"


szi*_*qui 7

一些用户将登陆此页面,搜索从xml在您的应用程序中使用自定义字体的说明.我是这样做的:

默认情况下,字体资源目录不存在.您可以按照此处文档手动创建它[太多工作]

或者使用GUI中的字体选择器

  • 在"设计"选项卡中选择TextView.

  • 打开fontFamilyxml属性的下拉列表.

  • 向下滚动到"更多字体".

  • 从显示的弹出窗口中选择"可下载的字体".

Android Studio将自动创建字体资源目录,其中包含上述文档中提到的必要声明.完成此步骤后,您可以在字体目录中复制自定义字体文件(例如myfont.ttf),并从xml中设置所需的字体,例如:

<TextView
     android:fontFamily="@font/myfont"
     android:id="@+id/aboutus_head_textview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/who_are_we" />
Run Code Online (Sandbox Code Playgroud)

如果要在整个应用程序中使用相同的字体,可以在styles.xml中设置fontFamily AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- custom font for the entire app -->
        <item name="fontFamily">@font/myfont</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

[前缀fontFamily android:如果上面没有工作]

截图:

截图1

截图2

注意:此答案假设您使用的是Android Studio 3+并支持库版本26+