【问题标题】:How to apply custom fonts on android with androidx如何使用 androidx 在 android 上应用自定义字体
【发布时间】:2020-03-11 12:28:32
【问题描述】:

我想在我使用 androidx 的 android 应用上应用自定义字体(不支持库!!)

但是我通过搜索找到的所有方法都是支持库。

他们说使用 android.support.v7.widget.appcompatTextview。但是我必须使用androidx。

我尝试了很多东西

  1. 同时使用
<item name="android:fontFamily">@font/custom_font_family</item>
<item name="fontFamily">@font/custom_pen_font_family</item>
  1. 用于应用所有 API 级别。 /font 的 custom_font_family.xml
android:font="@font/nanumbarunpen"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/nanumbarunpen"
app:fontStyle="normal"
app:fontWeight="500" />
  1. 直接为小部件的属性设置值。
<TextView
android:fontFamily="@font/custom_font_family"
.../>

即使是那些尝试,也都被忽略了。

但是!预览正确显示自定义字体! (我的真实手机仍然没有变化......)

帮帮我....拜托,,,,....... TT

【问题讨论】:

  • 您是否以编程方式为该文本视图设置任何内容?就像一种风格。
  • 如果我使用 Typeface 将字体设置为 TextView,它正在工作。但实际上我需要更改整个应用程序的字体......所以这种方式有点困难

标签: java android xml fonts


【解决方案1】:

我之前没有注意到androidxsupport library 之间有任何区别。 您可以在res 下添加字体文件夹,导入您的字体并使用它们,如下所示:

      <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iransans_fanum"
        android:textColor="@color/white"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

您确定没有以编程方式设置任何样式吗?

【讨论】:

  • @你试过了吗?
  • 如果我使用 android.support.v7.widget.appcompatTextview,它会在充气时出错
  • @kim-seonggyu 你能发布你的java和xml布局吗?
猜你喜欢
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2011-02-27
相关资源
最近更新 更多