android - 如何在 xml 布局中使用 Roboto

标签 android android-layout roboto

我现在经常听到这样的话:“Roboto 是默认字体类型”。但是如何使用这种默认字体类型呢?我的具体意思是,在过去我下载 Assets 等的时候,我曾经必须指定诸如

<item name="typeface">roboto_bold</item>

想象一下我有兴趣使用 all of say(依次)

roboto_thin
 roboto_thin_italic
 roboto_light
 roboto_light_italic
 roboto_regular
 roboto_italic
 roboto_medium
 roboto_medium_italic
 roboto_bold
 roboto_bold_italic
 roboto_black
 roboto_black_italic
 roboto_condensed_light
 roboto_condensed_light_italic
 roboto_condensed_regular
 roboto_condensed_italic
 roboto_condensed_bold
 roboto_condensed_bold_italic
 roboto_slab_thin
 roboto_slab_light
 roboto_slab_regular
 roboto_slab_bold

如何在我的 xml 布局文件或 style.xml 文件中指定它们?

最佳答案

我已经找到了一些可能性

使用 fontfamily

最简单的方法是将 fontFamily 属性添加到您的特定 view 中,例如 TextView

根据How to change fontFamily of TextView in Android

From android 4.1 / 4.2 / 5.0, the following Roboto font families are available:

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

http://developer.android.com/reference/android/widget/TextView.html#attr_android:typeface

in combination with

android:textStyle="normal|bold|italic"

this 14 variants are possible:

  • Roboto regular
  • Roboto italic
  • Roboto bold
  • Roboto bold italic
  • Roboto-Light
  • Roboto-Light italic
  • Roboto-Thin
  • Roboto-Thin italic
  • Roboto-Condensed
  • Roboto-Condensed italic
  • Roboto-Condensed bold
  • Roboto-Condensed bold italic
  • Roboto-Medium
  • Roboto-Medium italic

您也可以使用以下代码以编程方式执行此操作:

textView.setTypeface(Typeface.create("sans-serif-thin", Typeface.NORMAL));

使用字体

可用的内置ibn字体有:

  • 正常
  • 没有
  • 衬线
  • 等宽

你可以像下面这样组合它们:

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

android:typeface .

使用 styles.xml

你在styles.xml`中设置样式是这样的:

<style name="boldText">
    <item name="android:textStyle">bold|italic</item>
    <item name="android:textColor">#FFFFFF</item>
</style>

要在 main.xml 布局文件中使用这种样式,只需使用:

style="@style/boldText"

组合文本属性

你可以像下面的代码一样混合 TextView 属性:

 android:fontFamily="serif" 
 android:textStyle="italic"

使用第三方库

Foundry - 通过 XML 布局和样式应用自定义字体。

android-typeface-helper - Android 字体助手

补充讲座

您可能还想了解 Roboto字体和 Typography Google 的设计指南。

类似的 StackOverflow 问题:

关于android - 如何在 xml 布局中使用 Roboto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444616/

相关文章:

Android TextView 跑马灯不工作

Android:保存新图像后刷新图库

android - android中的动态按钮问题

android - 从 Google Maps API v2 MapView 更新 Google Play 服务时崩溃

android - 如何禁用长按android的键盘

java - 我所有的布局 xml 文件都变成了自动生成的文件

css - 仅当从 Windows 查看实时站点时,Webapp Roboto 字体异常

html - Chrome 中的 Roboto 字体显示不正确

android - Android中的默认字体系列是什么?

java - 膨胀类 ja.burhanrashid52.photoeditor.PhotoEditorView 时出错