我试图在Android Studio中使用自定义字体,就像我们在Eclipse中所做的那样。但不幸的是,不知道在哪里放“资产”文件夹!


当前回答

Android 8.0 (API 26)引入了与字体相关的新功能。

1)字体可以作为资源使用。

2)可下载的字体。

如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。

在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。

有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts

配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。

有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android

其他回答

Android 8.0 (API 26)引入了与字体相关的新功能。

1)字体可以作为资源使用。

2)可下载的字体。

如果你想在android应用程序中使用外部字体,你可以在apk中包含字体文件或配置可下载的字体。

在APK中包含字体文件:您可以下载字体文件,保存在res/字体文件器中,定义字体族,并在样式中使用字体族。

有关使用自定义字体作为资源的更多详细信息,请参阅http://www.zoftino.com/android-using-custom-fonts

配置可下载字体:通过提供字体提供程序详细信息来定义字体,添加字体提供程序证书并在样式中使用字体。

有关可下载字体的详细信息,请参阅http://www.zoftino.com/downloading-fonts-android

现在有很多应用字体的方法,最简单的方法之一是这样的, 1)右键单击res文件夹 进入“新建> Android资源目录”。

2)从资源类型列表中选择字体,然后单击确定。

3)把你的字体文件放在字体文件夹里。

首先在字体文件夹中添加font.ttf文件。然后在onCreate方法中添加这一行

    Typeface typeface = ResourcesCompat.getFont(getApplicationContext(), R.font.myfont);
    mytextView.setTypeface(typeface);

这是我的xml

            <TextView
            android:id="@+id/idtext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:gravity="center"
            android:text="My Text"
            android:textColor="#000"
            android:textSize="10sp"
        />

您可以使用简单的EasyFonts第三方库来设置各种自定义字体到您的TextView。通过使用这个库,你应该不必担心下载和添加字体到assets/fonts文件夹。还有关于字体对象的创建。您将从创建资产文件夹也自由。

简单:

TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));

这个库提供了许多类型的字体。

首先创建资产文件夹,然后在其中创建字体文件夹。

然后你可以设置字体从资产或目录如下:

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

        TextView tv = (TextView) findViewById(R.id.custom);
        Typeface face = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");

        tv.setTypeface(face);

        File font = new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf");

        if (font.exists()) {
            tv = (TextView) findViewById(R.id.file);
            face = Typeface.createFromFile(font);

            tv.setTypeface(face);
        } else {
            findViewById(R.id.filerow).setVisibility(View.GONE);
        }
    }
}