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


当前回答

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

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

其他回答

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

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

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

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

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

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

芬兰湾的科特林回答

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

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

你好,这里我们有一个更好的方法应用在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"
    />

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

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