我该把字体文件放在安卓资源的哪个位置?

19

我应该把字体文件(TTF)放在res文件夹的哪个位置?

5个回答

17

使用自定义字体

第一步是选择您想要使用的字体。

第二步,在您的资产目录中创建一个 Fonts 文件夹,并将您的字体复制到该文件夹中。

enter image description here

NB:您可以将字体放置在资产文件夹的任何位置,但这是我使用的方法!

至此,设置就完成了,现在进入代码部分。

为了访问您的自定义字体,您必须使用 Android SDK 中的 Typeface 类来创建一个 Android 可以使用的 Typeface,然后适当地设置需要使用您的自定义字体的任何显示元素。 例如,您可以在主屏幕上创建两个文本视图,一个使用默认的 Android Sans 字体,另一个使用您的自定义字体。 布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/DefaultFontText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text." />
    <TextView
        android:id="@+id/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
        </TextView>
</LinearLayout>

加载并设置自定义字体的代码也很简单,如下所示。

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Typeface tf = Typeface.createFromAsset(getAssets(),
                "fonts/BPreplay.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

您可以查看结果:

输入图像描述


10
你可以在asset文件夹中创建字体(即asset/fonts/roboto.ttf)。
然后,为你的TextView创建一个适当的类:
// RobotoFont class
package com.my.font;
public class RobotoFont  extends TextView {
    public RobotoFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public RobotoFont(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public RobotoFont(Context context) {
        super(context);
    }
    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),    "fonts/Roboto-Bold.ttf"));
        }
        else if(style == Typeface.ITALIC)
        {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
        }
        else
        {
            super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
        }
    }
}

最后,更新你的布局:

//main.xml
//replace textview with package name com.my.font.RobotoFont
<com.my.font.RobotoFont
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:paddingBottom="2dip" />

1
最好创建一个通用的自定义TextView(例如称为TypeFaceTextView),您可以通过预定义的属性app:fontFamily指定自己的字体。 - MikeL

5

4

2

Android Studio 1.5.1版本开始,您可以:

  1. 右键点击您的app目录
  2. New > Folder(在列表底部,容易被忽略)> Assets Folder
  3. 大多数情况下,您可以将文件夹位置保留为默认值> 点击完成
  4. 将文件移动到新创建的assets文件夹中

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接