我应该将字体文件放在 Android 资源中的什么位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10920438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:28:49  来源:igfitidea点击:

Where should I place font file in Android resources?

androidfonts

提问by user1019901

Where in res folder should I put my font file (TTF)?

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

采纳答案by Munish Kapoor

You can create the font in asset folder (i.e asset/fonts/roboto.ttf).

您可以在资产文件夹(即资产/字体/roboto.ttf)中创建字体。

Then, create an appropriate class for your TextView:

然后,为您的 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"));
        }
    }
}

and finally, update your layout:

最后,更新您的布局:

//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" />

回答by K_Anas

Using Custom Font

使用自定义字体

First step is to pick a font that you want to use.

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

Second create a Fonts folder in your assets directory and copy your font there.

其次在您的资产目录中创建一个 Fonts 文件夹并将您的字体复制到那里。

enter image description here

在此处输入图片说明

NB:you can put your font everywhere on asssets folder but this is the way that i do!!

注意:你可以把你的字体放在assets文件夹的任何地方,但我就是这样做的!!

That's it for the setup, now on to the code.

这就是设置,现在进入代码。

To access your custom font, you have to use the Typeface class in the Android SDK to create a typeface that Android can use, then set any display elements that need to use your custom font appropriately. To demonstrate, you can for exemple create two text views on your main screen, one using the default Android Sans font, and the other using your custom font. The layout is below:

要访问您的自定义字体,您必须使用 Android SDK 中的 Typeface 类来创建 Android 可以使用的字体,然后适当地设置需要使用您的自定义字体的任何显示元素。例如,您可以在主屏幕上创建两个文本视图,一个使用默认的 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>

The code to load and set the custom font is straight forward as well, and is shown below.

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

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);
    }
}

you can see the result:

你可以看到结果:

enter image description here

在此处输入图片说明

回答by K-ballo

Not in the resfolder, but anywhere in the assetsfolder. Then you can use the createFromAssetstatic method from Typeface:

不在res文件夹中,而是在assets文件夹中的任何位置。然后您可以使用以下createFromAsset静态方法Typeface

http://developer.android.com/reference/android/graphics/Typeface.html#createFromAsset%28android.content.res.AssetManager,%20java.lang.String%29

http://developer.android.com/reference/android/graphics/Typeface.html#createFromAsset%28android.content.res.AssetManager,%20java.lang.String%29

回答by Volodymyr Kulyk

You can create fontsfolder in resourcesand use it directly in xml, since Android O.
Check out new Android O-- fonts feature.

您可以在其中创建fonts文件夹resources并直接在 xml 中使用它,因为Android O.
查看新的Android O- 字体功能

回答by mumush

As of Android Studio 1.5.1you can:

Android Studio 1.5.1 开始,您可以:

  1. Right click on your appdirectory
  2. New> Folder(this is near the bottom of the list and is easy to miss) > Assets Folder
  3. In most cases you can leave the folder location as the default > Click Finish
  4. Move your files into the newly created assetsfolder
  1. 右键单击您的app目录
  2. New> Folder(这在列表的底部附近,很容易错过)>Assets Folder
  3. 在大多数情况下,您可以将文件夹位置保留为默认值 > 单击完成
  4. 将您的文件移动到新创建的assets文件夹中