TextView字体未在执行时应用

时间:2017-09-29 10:01:41

标签: android textview

我选择了android:fontFamily="casual"到TxtView中,在编辑器中它可以,但是在启动应用程序时没有(在我的手机上没有模拟)

为什么?

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/r1"
android:visibility="visible"
tools:context="com.example.dev_1.myapplication1.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">

<TextView
    android:id="@+id/textView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="60dp"
    android:layout_weight="0.04"
    android:fontFamily="casual"
    android:text="This is my text"
    android:textAlignment="center"
    android:textColor="@android:color/holo_green_light"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintTop_toTopOf="parent">

</TextView>

<Button
    android:id="@+id/button1"
    style="@android:style/Widget.Holo.Light.Button"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:onClick="func_fiche1"
    android:text="xxxxx"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

<Button
    android:id="@+id/button2"
    style="@android:style/Widget.Holo.Light.Button"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    android:onClick="func_fiche2"
    android:text="xxxxxx"
    android:textColor="@android:color/white"
    app:layout_constraintBottom_toTopOf="@+id/button3"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

<Button
    android:id="@+id/button3"
    style="@android:style/Widget.Holo.Light.Button"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="39dp"
    android:onClick="func_fiche3"
    android:text="xxxxx"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.497"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

</android.support.constraint.ConstraintLayout>

Android studio 2.3.3

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.dev_1.myapplication1"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso- core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

1 个答案:

答案 0 :(得分:0)

创建自定义textview类,您可以为其提供任何字体

public class CustomTextView extends android.support.v7.widget.AppCompatTextView {

public CustomTextView(Context context) {
    super(context);
}

public CustomTextView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    retreiveAttributes(context,attrs);
}

public CustomTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    retreiveAttributes(context,attrs);

}

public void setFont(Context context,int value) {
    switch (value){
        case 0:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoRegular.ttf"));
            break;
        case 1:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoSemibold.ttf"));
            break;
        case 2:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoHeavy.ttf"));
            break;
        case 3:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"Mastoc_PersonalUseOnly.ttf"));
            break;
        case 4:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoBlack.ttf"));
            break;
        case 5:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoBold.ttf"));
            break;
        case 6:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoLight.ttf"));
            break;
        case 7:
            this.setTypeface(Typeface.createFromAsset(context.getAssets(),"LatoMedium.ttf"));
            break;


    }
}

public void retreiveAttributes(Context context, AttributeSet attribute) {
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attribute,
            R.styleable.CustomTextView,
            0, 0);

    try {
        int value = a.getInteger(R.styleable.CustomTextView_fontStyle, 0);
        setFont(context,value);
    } finally {
        a.recycle();
    }
}

}

layout.xml类中的

 <com.dashboard.android.utilities.util.customTextViews.CustomTextView
            android:layout_below="@+id/buttonlogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fontStyle="lato_regular"
            android:layout_centerHorizontal="true"
            android:text="@string/copyright"
            android:textSize="@dimen/text_13sp"
            />