Android Studio通过布局编辑器设置fontfamily

时间:2015-12-15 17:14:44

标签: android-layout android-fonts

目前我正在使用

为运行时设置字体
android:typeface

但该字体并未反映在Android Studio的布局编辑器中。我尝试使用与我在代码中使用的值相同的值设置android:fontFamily以及{{1}},但没有用。

是否可以让布局构建器反映自定义字体更改?

2 个答案:

答案 0 :(得分:3)

根据How to change fontFamily of TextView in Android

  

从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-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)
     

enter image description here

     

结合使用
android:textStyle="normal|bold|italic"
     

这14种变体是可能的:

     
      
  • Roboto定期
  •   
  • Roboto italic
  •   
  • Roboto bold
  •   
  • Roboto粗体斜体
  •   
  • Roboto-Light
  •   
  • Roboto-Light italic
  •   
  • Roboto-Thin
  •   
  • Roboto-Thin italic
  •   
  • Roboto-Condensed
  •   
  • Roboto-Condensed italic
  •   
  • Roboto-Condensed bold
  •   
  • Roboto-Condensed bold italic
  •   
  • 的Roboto培养基
  •   
  • Roboto-Medium italic
  •   

注意:还有robotium-thin个类型空间。

但是当我看到你想要使用自定义字体时,你也会发现:

  

Android不允许您从XML布局设置自定义字体。   相反,您必须将特定字体文件捆绑在应用程序的资产中   文件夹,并以编程方式设置它。类似的东西:

TextView textView = (TextView) findViewById(<your TextView ID>);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "<file name>");
textView.setTypeface(typeFace);
     

请注意,您只能在setContentView()之后运行此代码   调用。此外,Android只支持一些字体,应该是   以.ttf (TrueType).otf (OpenType)格式。有些人   字体可能无效。

     

This是一种绝对适用于Android的字体,您可以使用   这是为了确认您的代码是否有效,以防您的字体文件没有   Android支持。

请自由发言,如果这篇文章对您没有帮助

答案 1 :(得分:1)

当选择textAppearance时,您应该在右侧看到TextView属性。您可以选择预定义的文本外观,也可以展开组并仅指定字体系列。

text appearance settings in Layout Editor

相关问题