如何在android中将textfont更改为世纪哥特式

时间:2016-03-01 13:03:31

标签: android xml textview

所以字体有4或5种字体样式,如果我们需要其他字体样式。我发现textappearance可以做到但是怎么做?

2 个答案:

答案 0 :(得分:1)

在assets文件夹下创建一个名为fonts的文件夹,并将所有字体放入其中。 (文件夹名称可以是任何内容)

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#222222" >

    <TextView
        android:id="@+id/ghost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="70dip"
        android:gravity="center"
        android:textColor="#ef0000"
        android:layout_marginTop="50dip"
        android:text="ghost" />

</LinearLayout>

<强> SampleActivity.java

package com.example;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidExternalFontsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Font path
        String fontPath = "fonts/Face Your Fears.ttf";

        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        // Applying font
        txtGhost.setTypeface(tf);
    }
}

答案 1 :(得分:0)

首先创建assets文件夹,然后在assets文件夹中创建fonts文件夹。

   TypeFace customTypeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf");
Textview.setTypeface(typeFace);