计时器字体在安卓中无法更改

3
我想要在Android中更改我的秒表字体。我已将默认字体设置为自定义字体。除了秒表之外,似乎到处都应用了该字体。我还尝试设置android:fontFamily="@font/myfont",但没有起作用。虽然字体在预览窗口中似乎已应用,但在我运行应用程序时它并没有改变。
有什么办法可以解决吗?
以下是我的代码:
<Chronometer
            android:fontFamily="@font/linotte_semibold"
            android:id="@+id/audio_player_chronometer"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_below="@+id/frame_layout"
            android:textColor="@color/white"
            android:layout_margin="5dp"
            />

我为你搜索了一个解决方案,并发现人们通常会创建一个自定义的文本视图来实现这个。请查看Custom font for clock textview - Gk Mohammad Emon
2个回答

6

你可以从Google Material中下载字体

  1. 进入布局编辑器
  2. 选择你的view并在右侧选择All Attributes,找到fontFamily

默认字体的fontFamily属性

  1. 现在点击下箭头,它将打开一个用于选择字体的窗口

Google Fonts资源

  1. 找到你想要的字体,你也可以搜索并点击ok

  2. 你的Chronometer看起来会不同

更改后的字体预览

这里是代码

<Chronometer
    android:id="@+id/chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:fontFamily="@font/allan_bold"
    android:textColor="@color/colorAccent"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

如果字体没有改变,在您的Activity.class中使用以下方法

Chronometer chronometer = findViewById(R.id.chronometer);

chronometer.setTypeface(ResourcesCompat.getFont(this, R.font.allan_bold));
//replace allan_bold with your desired font 

编辑 1 这是设备的输出结果 设备输出

希望这有所帮助!


1
工作了!但只有编程版本。 - goraga1

1
我只是按照以下步骤来将字体添加到Android中 https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html
  1. 将.ttf / .otf文件复制到“res/font”文件夹中
  2. 使用样式和权重详细信息生成字体资源文件。
  3. 将新字体设置为android:fontFamily="@font/sample_font"

请查看下面的示例代码,与添加字体和未添加字体进行比较

<!--With Font-->
<Chronometer
    android:id="@+id/chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/framelayout"
    android:layout_margin="5dp"
    android:fontFamily="@font/sample_font"
    android:textColor="@android:color/holo_blue_dark"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/audio_player_chronometer"
    app:layout_constraintVertical_bias="0.528" />

<!--Without Font-->
<Chronometer
    android:id="@+id/audio_player_chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/framelayout"
    android:layout_margin="5dp"
    android:textSize="30sp"
    android:textColor="@android:color/holo_blue_dark"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/framelayout" />

<!--sample text-->
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:fontFamily="@font/sample_font"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

截图 在此输入图片描述


同时,还要检查您使用字体设置的数字,大多数情况下,数字的样式与默认字体没有太大区别。 - UdayaLakmal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接