Android 执行时未应用TextView字体
我在TxtView中选择了Android 执行时未应用TextView字体,android,textview,Android,Textview,我在TxtView中选择了android:fontfamine=“casual”,在编辑器中可以,但在启动应用程序时不行(不是模拟的,在我的手机上) 为什么? 我的布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app
android:fontfamine=“casual”
,在编辑器中可以,但在启动应用程序时不行(不是模拟的,在我的手机上)
为什么?
我的布局文件:
<?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>
创建自定义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"
/>
您的android版本是什么?如果有,请添加代码和异常详细信息谢谢,我应该在CustomTextView类中进行导入吗?
<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"
/>