如何在.xml文件而不是.java文件中设置自定义字体?

时间:2016-04-07 10:07:28

标签: android

我正在创建一个我想要设置自定义字体的应用程序。

但是我不能在.xml文件中使用自定义字体,为了使用它,我需要初始化.java文件中的每个TextView。

这个过程太漫长而且耗时。

如果有人知道那么请帮助我......

9 个答案:

答案 0 :(得分:30)

Android API 16 +

从Android 8.0(API 26)开始,本机支持在XML中设置字体。但是,使用支持库将其扩展到Android 4.1(API 16)。

enter image description here

1。在项目中添加字体

  • 右键单击res文件夹,然后转到新建> Android资源目录。输入font作为名称,输入 font 作为资源类型。
  • 将字体复制并粘贴到新的res/font目录中。我只是在我的示例中使用单一字体,即常规dancing script字体。我将其重命名为dancing_script.ttf只是为了避免大写或非法字符的任何潜在命名问题。

2。创建font-family XML文件。

  • 右键单击res/font文件夹,然后选择New > Font Resource File。随便打电话给你。我打电话给我my_custom_font.xml
  • 粘贴以下代码。请注意,属性设置两次,一次用于android(API 26+),一次用于app(API 16 +)。

    <?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">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/dancing_script"
            app:fontStyle="normal"
            app:fontWeight="400"
            app:font="@font/dancing_script"
            />
    </font-family>
    

3。设置XML格式

现在您可以使用fontFamily属性以XML格式设置字体。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:fontFamily="@font/my_custom_font" />

注释

答案 1 :(得分:23)

供您参考,

 public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
        setTypeface(tf ,1);

    }
}

在XML中,

 <you_package.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:textSize="20sp"
        android:text="Your text"
        />

答案 2 :(得分:5)

使用提供此功能的library,例如Calligraphy

或者,使用Google的数据绑定库将字体“绑定”到您的小部件,如Lisa Wray所述。

答案 3 :(得分:5)

根据documentation

  

Android 8.0(API级别26)引入了一项新功能,即XML字体,   它允许您使用字体作为资源。您可以添加字体文件   res / font /文件夹将字体捆绑为资源。这些字体是   在您的R文件中编译,并在Android中自动提供   工作室。您可以在新的帮助下访问字体资源   资源类型,字体。例如,要访问字体资源,请使用   @ font / myfont,或R.font.myfont。要使用“在XML中使用字体”功能   运行Android API 14及更高版本的设备,请使用支持   图书馆26。

     

要将字体添加为资源,请在Android中执行以下步骤   工作室:

     

1.右键单击res文件夹,然后转到新建&gt; Android资源目录。将出现“新资源目录”窗口。

     

2.在“资源类型”列表中,选择“字体”,然后单击“确定”。注意:资源目录的名称必须是font。

     

3.在字体文件夹中添加字体文件。

     

创建字体系列

     

字体系列是一组字体文件及其样式和重量   细节。在Android中,您可以创建一个新的字体系列作为XML   资源并将其作为单个单元访问,而不是引用每个单元   作为独立资源的风格和重量。通过这样做,系统可以   根据您尝试使用的文本样式选择正确的字体。

     

要创建字体系列,请在Android中执行以下步骤   工作室:

     

1.右键单击字体文件夹,然后转到新建&gt;字体资源文件。将出现“新建资源文件”窗口。

     

2.输入文件名,然后单击“确定”。新的字体资源XML在编辑器中打开。

     

3.包含元素中的每个字体文件,样式和权重属性。以下XML说明了添加与字体相关的属性   在字体资源XML中:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>
     

在XML布局中使用字体

     

在布局XML文件中,将fontFamily属性设置为字体文件   你想要访问。

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>
     

打开“属性”窗口以设置TextView的字体。选择一个   查看以打开“属性”窗口。注意:“属性”窗口是   仅在设计编辑器打开时可用。选择“设计”选项卡   在窗口的底部。

     

展开textAppearance属性,然后从中选择字体   fontFamily list。

     

将字体添加到样式

     

打开styles.xml,并将fontFamily属性设置为字体文件   你想要访问。

 <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/lobster</item>
</style>

答案 4 :(得分:2)

您可以简单地使用自定义文本视图,如

public class HelveticaRagularTextView extends TextView {

public HelveticaRagularTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

public HelveticaRagularTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);

}

public HelveticaRagularTextView(Context context) {
    super(context);
    init(null);
}

private void init(AttributeSet attrs) {
    // Just Change your font name
    Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), getContext().getResources().getString(R.string.font_helvetica_regular));
    setTypeface(myTypeface);
}

}

现在你可以在你的xml中使用HelveticaRagularTextView。

这很容易使用

答案 5 :(得分:0)

InterruptedException扩展自定义类并自定义类中的字体。然后,您就可以在TextView中使用该自定义textview类,并且您不需要在代码中动态自定义字体。

祝你好运。

答案 6 :(得分:0)

下载您要使用的字体的ttf文件并将其粘贴到

  

SRC-&GT; main-&GT;资产 - &GT; font.ttf

然后使用ttf,您需要执行以下操作

用于将该字体设置为特定文本后执行

   TextView tv_the = (TextView) findViewById(R.id.the);
    Typeface face = Typeface.createFromAsset(getAssets(), "font.ttf");
    tv_the.setTypeface(face);

如果要将自定义字体设置为整个活动,请执行以下操作

final ViewGroup mContainer = (ViewGroup) findViewById(android.R.id.content).getRootView();
        final Typeface mFont = Typeface.createFromAsset(getAssets(), "badoni2.ttf");
        Parameters.setAppFont(mContainer, mFont);

并在您的应用中添加参数

public class Parameters {

public static final void setAppFont(ViewGroup mContainer, Typeface mFont) {
    if (mContainer == null || mFont == null)
        return;

    final int mCount = mContainer.getChildCount();

    // Loop through all of the children.
    for (int i = 0; i < mCount; ++i) {
        final View mChild = mContainer.getChildAt(i);
        if (mChild instanceof TextView) {
            // Set the font if it is a TextView.
            ((TextView) mChild).setTypeface(mFont);
        } else if (mChild instanceof ViewGroup) {
            // Recursively attempt another ViewGroup.
            setAppFont((ViewGroup) mChild, mFont);
        }

    }
}

试试这个,让我知道它是否适合你

答案 7 :(得分:0)

Android 8.0(API级别26)和Android支持库26引入了对API的支持,以从提供商应用程序请求字体,而不是将文件捆绑到APK中或让APK下载字体。可通过支持库26在运行Android API版本14和更高版本的设备上使用此功能。

请点击此链接 https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts

答案 8 :(得分:0)

如其他答案所示,您可以使用从Google下载的字体: Android Api 16+

在设计器中打开textAppearance微调器: 为视图选择一种字体(点击更多字体...),如下所示:

enter image description here


在对话框中,选择您的自定义字体并将其下载到您的项目中:
enter image description here

现在,您可以使用fontFamily属性从xml引用此字体:

    <Button
    android:id="@+id/btnTapMe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_button"
    android:fontFamily="@font/aclonica"

    android:text="Tap Me!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

作为奖励,您还可以从代码中使用它: 参见摘要:

Toast toast = Toast.makeText(this, yourString, Toast.LENGTH_SHORT);
LinearLayout layout= (LinearLayout) toast.getView();
TextView tvToast = (TextView) layout.getChildAt(0);

//Here is the relevant part:
Typeface typeface = ResourcesCompat.getFont(this, R.font.aclonica);
tvToast.setTypeface(typeface);
toast.show();