通过xml设置自定义字体[英] set custom font through xml

本文是小编为大家收集整理的关于通过xml设置自定义字体的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何设置一个字体,其TTF通过XML驻留在我的assets文件夹中? 我知道如何以编程方式进行操作但是如何通过XML做到这一点?预先感谢.

推荐答案

您不能直接使用XML进行操作,但是您可以扩展TextView并设置默认字体.

package com.nannu;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NanTV extends TextView{

    private Context c;
    public NanTV(Context c) {
        super(c);
        this.c = c;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }
    public NanTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);
        // TODO Auto-generated constructor stub
    }

    public NanTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }


}

在您的布局中使用新TextView

<com.nannu.NanTV
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 

        />

我正在从我的prev回答 https://stackoverflow.com/a/11239305/11666537 p>

其他推荐答案

如果您创建自己的TextView派生,则可以添加类XML-attribute,该类可以处理该程序的方式.这样,您可以指定某个字体,并将其设置为运行时,但是您可以通过XML进行操作. :)

否则,没有已知的方法来实现要求的行为.

其他推荐答案

您可以通过编写自定义文本视图来做到这一点,否则您必须以编程方式设置它.有关更多详细信息,请参见此 link

本文地址:https://www.itbaoku.cn/post/102626.html

问题描述

how can i set a font, whose ttf resides in my assets folder through xml? I know how to do that programmatically but how can you do that via xml? Thanks in advance.

推荐答案

You cannot do it using XML directly, however you can extend TextView and set a default font.

package com.nannu;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NanTV extends TextView{

    private Context c;
    public NanTV(Context c) {
        super(c);
        this.c = c;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }
    public NanTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);
        // TODO Auto-generated constructor stub
    }

    public NanTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }


}

And in your layout use new TextView

<com.nannu.NanTV
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 

        />

I am posting this from my prev answer https://stackoverflow.com/a/11239305/1166537

其他推荐答案

If you create your own TextView derivation, you could add an XML-attribute that the class handles the programmatical way. That way, you specify a certain font and it'll be set runtime, but you do it via XML. :)

Otherwise, there's no known way to achieve the requested behaviour.

其他推荐答案

You can do this by writing your custom TextView, otherwise you will have to set it programmatically. For more details see this link