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


当前回答

如果你像我一样对Android非常陌生,这可能有点棘手。请务必致电:

TextView myTextView = (TextView) findViewById(R.id.textView);
Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/your font.ttf");
myTextView.setTypeface(typeface);

方法,例如onCreate。

其他回答

我想补充一下我对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

2021年更新:

在res文件夹中创建一个名为font的文件夹并复制您的字体

所有字体名称只能为:小写字母a-z、0-9或下划线。

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

编程使用:

textView.setTypeface(ResourcesCompat.getFont(context, R.font.abc_font))

对于Android Studio 4.2+,现在甚至有一个菜单选项:

使用支持库26.0(和Android O)字体可以轻松地从资源加载:

Typeface typeface = ResourcesCompat.getFont(Context context, int fontResourceId) 

方法的文档。

更多信息可以在这里找到。

首先在字体文件夹中添加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"
        />

在Project -> app(或您的app名称)-> src -> main ->右键单击->新建->目录中创建文件夹资产。 然后在资产中创建一个名为fonts的新目录。

将字体分配给textView:

TextView textView = (TextView) findViewById(R.id.your_textView);

final Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/your_font_name");

Your_font_name包含字体扩展名。