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


当前回答

我认为我们可以使用谷歌字体而不是下载。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 Studio中执行以下步骤:

1)右键单击res文件夹,进入New > Android资源目录。出现“新建资源目录”窗口。

2)在“资源类型”列表中选择“字体”,单击“确定”。

注意:资源目录名必须为字体。

3)在字体文件夹中添加你的字体文件。

在xml文件中添加所需视图中的字体:

注:但你需要做到以下几点:

Android Studio 3.0以上的金丝雀。 你的Activity扩展了AppCompatActivity。 像这样更新你的Gradle文件:

    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {        
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

buildtoolsVersion以上26和最低targetSdkVersion要求26

在构建中添加依赖项。gradle文件:

classpath 'com.android.tools.build:gradle:3.0.0-beta4'

gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

在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包含字体扩展名。

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

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

简单:

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

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

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

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

方法,例如onCreate。