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


当前回答

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

其他回答

有很多方法来设置自定义字体家族在字段和我使用如下。

添加字体作为资源,在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

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

简单:

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

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

我无法加载字体,因为我将字体文件命名为Poppins-Medium。Tff,重命名为poppins_medium。TFF对我很管用。 其余步骤保持不变:

在res文件夹下创建字体资源目录 将tff文件复制并粘贴到该目录中 然后在XML的TextView中使用fontFamily属性。 如果以上步骤不起作用,您可以使用此链接创建该字体的FontFamily

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

对于新读者

你可以使用这个库 Gloxey定制字体视图

gradle依赖

  dependencies{
           compile 'io.gloxey.cfv:custom-font-views:1.0.2'
    }

如何使用?

创建文件夹资产->字体。复制你的字体到字体文件夹。

使用属性app: font_name = "font_name_string"在视图上应用字体。

例子

   <!--Font Names in srings.xml-->
       <string name="aadhunik">aadhunik.ttf</string>
       <string name="kung_fool">kungfool.ttf</string>
       <string name="skrova">skrova.otf</string>
       <string name="painting_in_the_sun_light">painting_in_the_sun_light.ttf</string>

   <!--Include views in layout.xml-->
       <io.gloxey.cfv.CFTextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:text="Aadhunik"
       android:textColor="#ff00"
       android:textSize="40sp"
       app:font_name="@string/aadhunik" />

       <io.gloxey.cfv.CFButton
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Kung Fool"
       android:textColor="#154748"
       app:font_name="@string/kung_fool" />

       <io.gloxey.cfv.CFEditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:text="Hello world"
       android:textSize="30sp"
       app:font_name="@string/skrova" />

       <io.gloxey.cfv.CFCheckBox
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center"
       android:text="Painting In The Sun Light"
       android:textSize="30sp"
       app:font_name="@string/painting_in_the_sun_light" />