【问题标题】:How to set a custom font for android application?如何为android应用程序设置自定义字体?
【发布时间】:2017-07-09 21:51:54
【问题描述】:

我似乎无法在我的应用程序中将标准 android 字体更改为另一种字体。我正在用 Kotlin 编写我的应用程序,并且正在使用 Anko 进行布局。我试过了:

typeface = Typeface.create()
typeface = Typface.createFromAsset(assets, "font/font_name")
setTypeface(Typeface.createFromAsset(assets, "font/font_name"))

感谢您的帮助。

【问题讨论】:

标签: android kotlin anko


【解决方案1】:

我在使用 Kotlin 的 Android Studio 3.1 Canary 上遇到了同样的问题

这里是解决方案:对于 font/lobster_regular.ttf 文件

var typeFace: Typeface? = ResourcesCompat.getFont(this.applicationContext, R.font.lobster_regular)

示例:

private var _customFontTextView: TextView? = null
private var _typeFace:           Typeface? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    this.setContentView(R.layout.activity_main)

    this._initializeResources()
    this._initializeGUI()
}

private fun _initializeResources() {
    this._customFontTextView = this.findViewById(R.id.custom_font_text_view)
    this._typeFace           = ResourcesCompat.getFont(this.applicationContext, R.font.lobster_regular)
}

private fun _initializeGUI() {
    this._customFontTextView!!.setTypeface(this._typeFace, Typeface.NORMAL)
}

您还可以使用可下载字体做更多事情,这是来自 Google 的文章:https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html

【讨论】:

  • 如何为变量指定字体大小?
  • 我已经在 textview 上设置了字体,我相信你知道如何在 textview 上设置字体大小,对吧?
  • 我的意思是我们可以声明字体类型和大小的一种方法吗?
【解决方案2】:

试试这个,来自this tutorial from SEGUN

val myCustomFont : Typeface? = ResourcesCompat.getFont(this, R.font.my_font)
myView.typeface = myCustomFont

注意:您需要在您的项目中下载.ttf 字体,而不仅仅是可下载字体的.xml

【讨论】:

    【解决方案3】:

    我们可以为自定义 textView 创建类。例如我们需要带有roboto字体的textview

    class RobotoTextView(context: Context?, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
        init {
            val typeface = Typeface.createFromAsset(getContext().assets, "font/roboto.ttf")
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ||
                    android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
                setTypeface(typeface)
            }
        }
    }
    

    然后我们可以在每个 xml 布局上使用它

    <com.packagename.RobotoTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World"/>
    

    【讨论】:

    • 当我们在很多地方使用自定义字体时。这是最好的方法!
    【解决方案4】:

    这是一个很好的方法:
    1- 在 res 文件夹中创建一个名为 font 的文件夹。 2- 然后将您的字体文件放入您创建的 font 文件夹中。 (我们假设您的字体文件名是 sample_font.ttf
    3-所以现在在名为sample.xml的字体文件夹中创建一个xml文件并将以下代码放入其中

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            android:font="@font/sample_font"
            android:fontStyle="normal"
            android:fontWeight="500" />
    </font-family>
    

    4- 现在转到并打开res/values/styles.xml,然后在主样式中添加以下行代码(通用名称为AppTheme

    <item name="fontFamily">@font/sample</item>  //put name of xml file in the font folder
    

    现在运行你的应用,你会看到你的应用字体改变了。

    【讨论】:

      【解决方案5】:

      你可以试试这个,从我的工作代码(在 Kotlin 中)

      如果你在其他类文件中,那么使用这个

      var typeFaceBold: Typeface? =
          this.getApplicationContext()?.let { ResourcesCompat.getFont(it, R.font.sans_serif_bold) }
          tvAnonymousText.typeface = typeFaceBold
      

      如果是在Activity中使用,那么可以这样写

      var typeFaceBold: Typeface? = ResourcesCompat.getFont(this.applicationContext, R.font.sans_serif_bold) }
      tvAnonymousText.typeface = typeFaceBold
      

      【讨论】:

        猜你喜欢
        • 2018-03-24
        • 2012-08-30
        • 2016-02-28
        • 2012-08-04
        • 2011-11-28
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 2012-06-13
        相关资源
        最近更新 更多