Android: как установить широкоформатный шрифт приложения по умолчанию без переопределения textAppearance

Я использую собственный шрифт в своем приложении для Android. Я хочу установить этот пользовательский шрифт в качестве шрифта приложения по умолчанию (в качестве запасного варианта), но я все еще хочу использовать свойство TextView textAppearance для установки шрифта и стиля текста.

Похоже, если установить textViewStyle или fontFamily в базовой теме моего приложения, то allays переопределит стиль TextView textAppearance?

<style name="MyApp.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <!-- Overriding fontFamily also overrides textView textAppearance -->
    <item name="android:fontFamily">@font/open_sans</item>

    <!-- Overriding textViewStyle also overrides textView textAppearance -->
    <item name="android:textViewStyle">@style/DefaultTextAppearance</item>

</style>

 <style name="DefaultTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/open_sans_regular</item>
</style>

<style name="BoldTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/open_sans_extra_bold</item>
</style>


<!-- This textView does not get bold style -->
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Me happy"
  android:textAppearance="@style/BoldTextAppearance" />

person devha    schedule 14.12.2017    source источник


Ответы (1)


Атрибуты стиля представления имеют больший приоритет, чем textAppearance.

В обоих случаях у вас есть fontFamily внутри стиля вместо textAppearance. Когда вы помещаете fontFamily непосредственно в базовую тему, представление получает этот стиль из своей активности.

Итак, вместо того, чтобы устанавливать базовое значение fontFamily в стиле, вам нужно установить базовое TextView TextAppearance:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textViewStyle">@style/TextViewStyle</item>
</style>

<style name="TextViewStyle" parent="@android:style/TextAppearance.Widget.TextView">
    <item name="android:textAppearance">@style/DefaultTextAppearance</item>
</style>

<style name="DefaultTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/roboto_regular</item>
</style>

<style name="LobsterTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/lobster</item>
</style>

Итак, здесь у меня есть 2 textAppearances: DefaultTextAppearance, который используется по умолчанию для всех TextView, и LobsterTextAppearance, который я использую в конкретном случае.

Макет выглядит так:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Default roboto text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lobster text"
    android:textAppearance="@style/LobsterTextAppearance" />

Итак, первый TextView использует fontFamily из базового textAppearance, второй использует переопределенный @style/LobsterTextAppearance

fontFamily textПредварительный просмотр внешнего вида

person repitch    schedule 18.04.2018