Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字体更改为Xml中定义的默认字体?_Java_Android_Xml_Fonts - Fatal编程技术网

Java 如何将字体更改为Xml中定义的默认字体?

Java 如何将字体更改为Xml中定义的默认字体?,java,android,xml,fonts,Java,Android,Xml,Fonts,我已使用 Typeface font = Typeface.createFromAsset(this.getAssets(),"fonts/Arial.ttf"); 在我的Xml布局中,我有许多不同字体的文本视图,我希望随时以编程方式保留它们 尝试了以下方法 Typeface.DEFAULT 和字体字体=null 所有文本视图都设置为相同的字体,而不是Xml中的字体 如何在不重新启动应用程序的情况下保留字体?可能的解决方案是使用TypeFace类的静态实例。不久前,我在此线程中提出了此问

我已使用

Typeface font = Typeface.createFromAsset(this.getAssets(),"fonts/Arial.ttf"); 
在我的Xml布局中,我有许多不同字体的文本视图,我希望随时以编程方式保留它们

尝试了以下方法

Typeface.DEFAULT 
字体字体=null

所有文本视图都设置为相同的字体,而不是Xml中的字体


如何在不重新启动应用程序的情况下保留字体?

可能的解决方案是使用TypeFace类的静态实例。不久前,我在此线程中提出了此问题的解决方案:。我希望您觉得它有用。

一个可能的解决方案是使用TypeFace类的静态实例。不久前,我在此线程中提出了此问题的解决方案:。我希望您觉得它有用。

步骤1)创建一个类名为CustomTextView并复制粘贴此代码,但请确保自定义字体应为Project Assets文件夹。因此,您可以替换字体名称

打包你的包

public class CustomTextView extends TextView{

    public CustomButton(Context context) {
        super( context );
        setFont();

    }

    public CustomTextView (Context context, AttributeSet attrs) {
        super( context, attrs );
        setFont();
    }

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        setFont();
    }

    private void setFont() {
        Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf");
        setTypeface( normal, Typeface.NORMAL );

    }
}
步骤2)

在布局中使用此自定义文本视图

<LinearLayout
        android:id="@+id/signing_details"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
        android:weightSum="100" >

<YOUR_PACKAGE.CustomTextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:text="@string/app_name"
                 android:textSize="16sp" />
</ LinearLayout>
希望这对您有所帮助。

步骤1)创建一个类名为CustomTextView并复制粘贴此代码,但请确保自定义字体应为Project Assets文件夹。因此,您可以替换字体名称

打包你的包

public class CustomTextView extends TextView{

    public CustomButton(Context context) {
        super( context );
        setFont();

    }

    public CustomTextView (Context context, AttributeSet attrs) {
        super( context, attrs );
        setFont();
    }

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        setFont();
    }

    private void setFont() {
        Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf");
        setTypeface( normal, Typeface.NORMAL );

    }
}
步骤2)

在布局中使用此自定义文本视图

<LinearLayout
        android:id="@+id/signing_details"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
        android:weightSum="100" >

<YOUR_PACKAGE.CustomTextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:text="@string/app_name"
                 android:textSize="16sp" />
</ LinearLayout>
希望这对你有帮助