[英]Font family resource from theme not applied to TextViews
I want to set the android:textAppearance
of several TextViews
.我想设置几个
TextViews
的android:textAppearance
。 To do that, I have declared the following style:为此,我声明了以下样式:
<style name="TextAppearance.MyTheme.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
<item name="android:fontFamily">@font/montserrat</item>
<item name="android:textStyle">normal</item>
<item name="android:textFontWeight">400</item>
<item name="android:textSize">@dimen/text_size_215</item>
</style>
Said style will be set to the attr
of textAppearanceHeadline
in my theme like so:所述样式将在我的主题中设置为
textAppearanceHeadline
的attr
,如下所示:
<item name="textAppearanceHeadline1">@style/TextAppearance.MyTheme.Headline1</item>
The font family XML is written like so:字体系列 XML 是这样写的:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="200"
app:font="@font/montserrat_extralight" />
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="@font/montserrat_regular" />
<font
app:fontStyle="normal"
app:fontWeight="500"
app:font="@font/montserrat_medium" />
<font
app:fontStyle="normal"
app:fontWeight="600"
app:font="@font/montserrat_semibold" />
</font-family>
All the @font/montserrat_*
references are .ttf
files that I've included in my resources folder.所有
@font/montserrat_*
引用都是我的资源文件夹中包含的.ttf
文件。 Then, I will set the style using said attr
value to the TextView
I want to style like so:然后,我将使用上述
attr
值将样式设置为TextView
我想这样设置样式:
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writing Words"
android:textAppearance="?attr/textAppearanceHeadline1"
app:layout_constraintEnd_toEndOf="@+id/headingView"
app:layout_constraintStart_toStartOf="@+id/headingView"
app:layout_constraintTop_toBottomOf="@+id/headingView" />
The problem is, the font family and font-weight are applied incorrectly (they change to the default font family and font-weight).问题是,字体系列和字体粗细应用不正确(它们更改为默认字体系列和字体粗细)。 The size is applied correctly.
尺寸已正确应用。 Did I do something wrong here?
我在这里做错了吗?
UPDATE更新
When I try setting only either android:textAppearance
or style
to the TextView
with ?attr/textAppearanceHeadline1
, it doesn't set the font-family and font-weight.当我尝试使用
?attr/textAppearanceHeadline1
仅将android:textAppearance
或style
设置为TextView
时,它不会设置字体系列和字体粗细。 However, when I set android:fontFamily
and android:fontWeight
alongside android:textAppearance
, it works.但是,当我将
android:fontFamily
和android:fontWeight
与android:textAppearance
一起设置时,它可以工作。 Setting those alongside style
does not work.将这些设置在
style
旁边是行不通的。 Why is that so?为什么呢? What's happening?
发生了什么?
Apparently, I have to override fontFamily
also.显然,我也必须覆盖
fontFamily
。 Overriding fontFamily
in my style makes it work flawlessly.以我的风格覆盖
fontFamily
使它完美地工作。 The style becomes the following.样式变成如下。
<style name="TextAppearance.MyTheme.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
<item name="fontFamily">@font/montserrat</item>
<item name="android:fontFamily">@font/montserrat</item>
<item name="android:textStyle">normal</item>
<item name="android:textFontWeight">400</item>
<item name="android:textSize">@dimen/text_size_215</item>
</style>
Probably in your xml exist viewGroup (where your's TextView one of the child) with style that have android:fontFamily
that override your's android:textAppearance
Probably in your xml exist viewGroup (where your's TextView one of the child) with style that have
android:fontFamily
that override your's android:textAppearance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.