在Android的自定义字体(整个应用程序)

问题描述:

我有这样的Andr​​oid code有许多类各有不同的看法。我们可以去设置,并根据选择的字体更改其字体。现在只有pre-安装android的字体可用。有没有一种方法略有调整我的code,这样我可以添加一个.TTF文件,并​​提供了在字体的选择。 我不想做出大的变化,code。

I have this android code with many classes each with different Views. We can go to settings and change their font according to the font selected. Right now only the pre-installed android fonts are available. Is there a way to slightly tweak my code so that I can add a .ttf file and provide that as an option in fonts. I don't want to make large changes in code.

您可以使用的字体设置自定义字体在TextView中的文本。所以每当你需要自定义字体为您的TextView,你可以使用下面的。

You can use typeface to set custom font for the text in textview. So whenever you required custom font for your textview you can use the below.

activity_main.xml

activity_main.xml

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
<TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />
 </LinearLayout>

在code:

The code:

public class MainActivity extends Activity {

private TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener( new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            text.setText("This is a custom toast");
            Typeface typeFace =  Typeface.createFromAsset(getAssets(),"fonts/kn.ttf");
            text.setTypeface(typeFace);

        }

    });

}
}