简体   繁体   中英

Custom fonts with android studio is not working on devices

I'm trying to apply a custom fonts on my android app not only a few widgets also whole texts.

So I made a font resource directory and added font file ( .otf but I have tried also .ttf ),

custom-font-family.xml

And then, I added font_style.xml in value directory referenced by App Theme of style.xml

So I could saw it is working in preview...

But in my real device, It is not working!!

It display basic font of device setting.

my code is here

/font/nanum_barun_pen_font_family.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">

<!--For API 26 ++ -->
<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/nanumbarunpenr" />

<!--For under API 26-->
<font
    app:fontStyle="normal"
    app:fontWeight="400"
    app:font="@font/nanumbarunpenr"/>
</font-family>
/res/value/font_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Custom font 적용 -->
    <style name="customTextViewFontStyle" parent="@android:style/Widget.DeviceDefault.TextView">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>

    <style name="customButtonFontStyle" parent="@android:style/Widget.DeviceDefault.Button.Borderless">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>

    <style name="customEditTextFontStyle" parent="@android:style/Widget.DeviceDefault.EditText">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>

    <style name="customRadioButtonFontStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.RadioButton">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>

    <style name="customCheckboxFontStyle" parent="@android:style/Widget.DeviceDefault.CompoundButton.CheckBox">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>

    <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/nanum_barun_pen_font_family</item>
    </style>
</resources>

/res/values/style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/red</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--Apply custom font-family-->
        <item name="android:textViewStyle">@style/customTextViewFontStyle</item>
        <item name="android:buttonStyle">@style/customButtonFontStyle</item>
        <item name="android:editTextStyle">@style/customEditTextFontStyle</item>
        <item name="android:radioButtonStyle">@style/customRadioButtonFontStyle</item>
        <item name="android:checkboxStyle">@style/customCheckboxFontStyle</item>
    </style>

</resources>

gradle

...
ext {
    supportLibraryVersion = '28.0.0'
    GsonVersion = '2.8.6'
    retrofitVersion = '2.7.1'
}

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "..."
        minSdkVersion 21
        targetSdkVersion 29
...

I tried apply this way on new empty android project.(same sdk version) but It is working well on this..

I don't know why it is not working only this app!!

Finally, I tried apply font-family directly, it was same.

Why !?!?

Please help me

If you are using TextView then change your TextView to android.support.v7.widget.AppCompatTextView. And should work.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM