Android Custom Font only appears in layout preview

2019-08-29 05:04发布

问题:

I am applying a custom font on a textview in Android fragment. The font shows in layout design preview but not on real device. Font .ttf is in res>font folder.

The font resource file is jameel.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">

    <!-- regular -->
    <font
        android:font="@font/jameel_noori"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/jameel_noori"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <!-- italic -->


</font-family>

Textview in fragment layout:

<TextView
            android:id="@+id/tv_urdu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:lineSpacingExtra="10dp"
            android:paddingTop="6dp"
            android:text="@string/sample_arabic_text"
            android:textAlignment="gravity"
            android:textColor="@color/black"
            android:textSize="26sp"
            android:fontFamily="@font/jameel"/>

If I change android:fontFamily="@font/jameel" to app:fontFamily="@font/jameel" the build fails with the error:

C:\Users\Zoha\StudioProjects\SonayJagnayKayAzkaar1\app\src\main\res\layout\fragment_single_dua.xml:88: error: attribute fontFamily (aka com.alhuda.duasapp.sonayjagnaykayazkaar:fontFamily) not found.
error: failed linking file resources.

My build.gradle is:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "28.0.3"

    defaultConfig {
        applicationId "com.alhuda.duasapp.sonayjagnaykayazkaar"
        minSdkVersion 17
        targetSdkVersion 27
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    implementation 'com.android.support:support-v4:27.0.2'

}