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


当前回答

你好,这里我们有一个更好的方法应用在android上的EditTexts和TextViews字体,并将其应用于整个项目。

首先,你需要使字体文件夹。以下是步骤。

1:进入(项目文件夹)然后应用>src>main

2:在主文件夹中创建名为assets/fonts的文件夹。

3:把你的字体放进字体文件夹。这里是' mavenpro - regular。ttf'

下面是在EditText上应用自定义字体的步骤,使用这种方法可以在每个输入上应用字体。

1:创建一个类MyEditText(你喜欢的名字…)

2:扩展EditText

3:使用你的字体

下面是代码示例;

public class MyEditText extends EditText {

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

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

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

    private void init() {
        if (!isInEditMode()) {
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/MavenPro-Regular.ttf");
            setTypeface(tf);
        }
    }

}

这里是如何使用它的代码。

MyEditText editText = (MyEditText) findViewById(R.id.editText);

editText.setText("Hello");

或者在你的xml文件中

   <MyEditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textColor="#fff"
    android:textSize="16dp"
    android:id="@+id/editText"
    />

其他回答

我认为我们可以使用谷歌字体而不是下载。ttf文件。这很容易实现。你只需要遵循这些步骤。 步骤1)打开你的项目的layout.xml和属性中的文本视图的选择字体家族(作为参考截图附在后面)

步骤2)in字体家族选择更多字体..选项,如果你的字体不在那里。然后你会看到一个新的窗口将打开,在那里你可以输入你需要的字体,并从列表中选择所需的字体,即常规,粗体,斜体等。如下图所示。

步骤3)然后你会看到一个字体文件夹将自动生成在/res文件夹有你选择的字体xml文件。

然后你可以直接在xml中使用这个字体家族作为

      android:fontFamily="@font/josefin_sans_bold"

或者从专业语法上讲,你可以使用

  Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
  fontText.setTypeface(typeface);

我想补充一下我对Android- o和Android Studio 2.4的回答

在res文件夹下创建名为font的文件夹。下载要添加到项目示例谷歌fonts中的各种字体 在xml用户字体家族中 例子: < TextView android: fontFamily = " @font / indie_flower” android: layout_width = " wrap_content " android: layout_height = " wrap_content " android:填充= " 10 dp” android:文本= " @string / sample_text " / >

3.如果您希望它以编程的方式使用下面的代码

Typeface typeface = getResources().getFont(R.font.indie_flower);
textView.setTypeface(typeface);

要了解更多信息,请链接到我的博客文章字体样式的Android与Android Studio 2.4

芬兰湾的科特林回答

如果你需要在代码方面使用字体,你可以使用这个功能,它也有版本代码控制。

fun getFontJarvisWhite(): Typeface {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) resources.getFont(R.font.jarvis_white)
    else context?.let { ResourcesCompat.getFont(it, R.font.jarvis_white) }!!
}

把字体放在资产文件夹 然后应用fontfamily: " your fonts

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

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

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