Android 我如何指定例如。style.xml 中的 Roboto-Medium 或 Roboto-Black

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21023304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:50:59  来源:igfitidea点击:

How do I specify eg. Roboto-Medium or Roboto-Black in styles.xml

androidandroid-fontsandroid-typeface

提问by ckibsen

Posts like this How to change fontFamily of TextView in Androidsuggests, that the variants of Roboto fonts you can specify in styles.xml in Android 4.2 boils down to the following:

像这样的帖子How to change fontFamily of TextView in Android表明,您可以在Android 4.2的styles.xml中指定的Roboto字体变体归结为以下几点:

  • Regular
  • Italic
  • Bold
  • Bold-italic
  • Light
  • Light-italic
  • Thin
  • Thin-italic
  • Condensed regular
  • Condensed italic
  • Condensed bold
  • Condensed bold-italic
  • 常规的
  • 斜体
  • 胆大
  • 加粗斜体
  • 浅斜体
  • 薄的
  • 细斜体
  • 浓缩正则
  • 浓缩斜体
  • 浓缩粗体
  • 浓缩粗斜体

That leaves out the ability to style TextViews using eg. the Roboto-Medium or Roboto-Black fonts.

这排除了使用例如设置 TextViews 样式的能力。Roboto-Medium 或 Roboto-Black 字体。

But why would Google add system wide fonts that can not be used for styling of your TextViews? Surely there must be some way of specifying all of the Roboto fonts from within styles.xml (ie. NOT having to embed the fonts as Assets and creating custom TextViews in code) - but how?

但是为什么 Google 会添加不能用于 TextView 样式的系统范围字体?当然必须有某种方式从styles.xml 中指定所有Roboto 字体(即不必将字体作为资产嵌入并在代码中创建自定义TextViews) - 但如何?

回答by rciovati

On Android 5.0 you can set Roboto Medium with sans-serif-medium.

在 Android 5.0 上,您可以使用sans-serif-medium.

This solution, taken from Google iosched 2014, uses sans-serifon Android pre-v21:

此解决方案取自Google iosched 2014,用于sans-serifAndroid pre-v21:

values/styles.xml

值/样式.xml

<style name="MyStyle">
    <item name="android:fontFamily">@string/font_fontFamily_medium</item>
</style>

values/fonts.xml

值/fonts.xml

<string name="font_fontFamily_medium">sans-serif</string>

values-v21/fonts.xml

值-v21/fonts.xml

<string name="font_fontFamily_medium">sans-serif-medium</string>

回答by Abror Esonaliyev

NEW SOLUTION

新解决方案

After Google I/O '17you can create multiple font family in res/fontfolder

Google I/O '17 之后,您可以在res/font文件夹中创建多个字体系列

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
    app:font="@font/roboto_light"
    app:fontStyle="normal"
    app:fontWeight="300" />
<font
    app:font="@font/roboto_bold"
    app:fontStyle="normal"
    app:fontWeight="700" />

</font-family>

and you can use wherever want

并且你可以使用的地方要

 <style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView">
    <item name="android:textColor">@color/colorBlack</item>
    <item name="android:fontFamily">@font/font_family_roboto</item>
</style>

font folderfont family

字体文件夹字体系列

OLD ANSWER

旧答案

Thanks for Jakob Eriksson

感谢雅各布·埃里克森

original answer

原答案

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 style:

很有型:

<style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView">
    <item name="android:textSize">@dimen/text_view_text_size</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
</style>

in app theme:

在应用主题中:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/gray</item>
    <item name="android:textViewStyle">@style/AppTheme.DefaultTextView</item>
</style>

回答by Rampo

I suggest you to use custom libraries like Android-RobotoTextViewor Calligraphy. With one of these you can set the font of a view in xml with an attribute so you can put in in styles.xml . And they works with previous version of Android than 4.0

我建议您使用自定义库,如Android-RobotoTextViewCalligraphy。使用其中之一,您可以使用属性在 xml 中设置视图的字体,以便您可以将其放入 styles.xml 中。它们适用于 4.0 之前的 Android 版本

回答by Andrew Orobator

Support Library 26 introduced using fonts in XML, and it's backwards compatible to Android API 14.

使用 XML 中的字体引入了支持库 26 ,并且它向后兼容 Android API 14。

To add fonts as resources, perform the following steps in the Android Studio:

要将字体添加为资源,请在 Android Studio 中执行以下步骤:

  1. Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.

  2. In the Resource type list, select font, and then click OK. Note: The name of the resource directory must be font.

  1. 右键单击 res 文件夹,然后转到新建 > Android 资源目录。出现“新建资源目录”窗口。

  2. 在资源类型列表中,选择字体,然后单击确定。注意:资源目录的名称必须是字体。

Figure 1. Adding the font resource directory

图 1. 添加字体资源目录

  1. Add your font files in the font folder. The folder structure below generates R.font.dancing_script, R.font.lobster, and R.font.typo_graphica.
  1. 在字体文件夹中添加您的字体文件。下面的文件夹结构生成R.font.dancing_scriptR.font.lobsterR.font.typo_graphica

Figure 2. Adding the font files in the resource directory

图 2. 在资源目录中添加字体文件

  1. Double-click a font file to preview the file's fonts in the editor.
  1. 双击字体文件可在编辑器中预览文件的字体。

Figure 3. Previewing the font file

图 3. 预览字体文件

To set a font for the TextView, in the layout XML file, set the fontFamily attribute to the font file you want to access.

要为 设置字体TextView,请在布局 XML 文件中,将 fontFamily 属性设置为要访问的字体文件。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

The Android Studio layout preview allows you to preview the font set in the TextView.

Android Studio 布局预览允许您预览在 TextView 中设置的字体。

Figure 4. Previewing fonts in layout preview

图 4. 在布局预览中预览字体

To add a font to a style, Open the styles.xml, and set the fontFamily attribute to the font file you want to access.

要将字体添加到样式,请打开styles.xml,并将fontFamily 属性设置为要访问的字体文件。

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>

If you don't want to bundle the fonts in your app, you can always look into Downloadable Fonts

如果您不想在应用程序中捆绑字体,您可以随时查看可下载字体