Android 简明教程
Android - Custom Fonts
在 Android 中,可以为应用程序中的字符串定义自己的自定义字体。只需要从互联网上下载所需的字体,然后将其放在 assets/fonts 文件夹中。
In android, you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.
将字体放在 fonts 文件夹下的 assets 文件夹中后,可以通过 Typeface 类在你的 java 代码中访问该字体。首先,在代码中获取文本视图的引用。其语法如下 -
After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First , get the reference of the text view in the code. Its syntax is given below −
TextView tx = (TextView)findViewById(R.id.textview1);
接下来需要做的就是调用 Typeface 类 createFromAsset() 的静态方法来从 assets 中获取自定义字体。其语法如下 -
The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. Its syntax is given below −
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");
最后需要做的就是将该自定义字体对象设置到你的 TextView Typeface 属性。需要调用 setTypeface() 方法来执行该操作。其语法如下 -
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below −
tx.setTypeface(custom_font);
除了这些方法之外,在 Typeface 类中还定义了其他方法,可更有效地处理字体。
Apart from these Methods, there are other methods defined in the Typeface class , that you can use to handle Fonts more effectively.
Sr.No |
Method & description |
1 |
create(String familyName, int style) Create a Typeface object given a family name, and option style information |
2 |
create(Typeface family, int style) Create a Typeface object that best matches the specified existing Typeface and the specified Style |
3 |
createFromFile(String path) Create a new Typeface from the specified font file |
4 |
defaultFromStyle(int style) Returns one of the default Typeface objects, based on the specified style |
5 |
getStyle() Returns the Typeface’s intrinsic style attributes |
Example
以下是一个演示如何使用 Typeface 处理 CustomFont 的示例。它创建了一个基本应用程序,该应用程序显示您在字体文件中指定的自定义字体。
Here is an example demonstrating the use of Typeface to handle CustomFont. It creates a basic application that displays a custom font that you specified in the fonts file.
要试验此示例,您可以在实际设备或模拟器上运行它。
To experiment with this example, you can run this on an actual device or in an emulator.
Steps |
Description |
1 |
You will use Android studio IDE to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 |
Download a font from internet and put it under assets/fonts folder. |
3 |
Modify src/MainActivity.java file to add necessary code. |
4 |
Modify the res/layout/activity_main to add respective XML components |
5 |
Run the application and choose a running android device and install the application on it and verify the results |
在进入代码部分之前,请从 Windows 资源管理器向资产文件夹中添加字体。
Before entering to code part add fonts in assests folder from windows explorer.
以下是修改后的主活动文件内容 MainActivity.java 。
Following is the content of the modified main activity file MainActivity.java.
package com.example.sairamkrishna.myapplication;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView tv1,tv2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.textView3);
tv2=(TextView)findViewById(R.id.textView4);
Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
tv1.setTypeface(face);
Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
tv2.setTypeface(face1);
}
}
下面是经过修改的 xml 的内容 activity_main.xml 。
Following is the modified content of the xml activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Typeface"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:textSize="45dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3"
android:layout_marginTop="73dp"
android:textSize="45dp" />
</RelativeLayout>
以下是 res/values/string.xml 的内容。
Following is the content of the res/values/string.xml.
<resources>
<string name="app_name">My Application</string>
</resources>
下面是 AndroidManifest.xml 文件的内容。
Following is the content of AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行我们刚刚修改过的 Custom Font 应用程序。我假设您在进行环境设置时已创建了 AVD 。要从 Android 工作室运行此应用,请打开一个项目的活动文件并单击工具栏中的运行图标。Android 工作室将在您的 AVD 上安装该应用并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 -
Let’s try to run our Custom Font application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project’s activity files and click Run icon from the toolbar.Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −
正如您所看到的,显示在 AVD 上的文本并非默认的 Android 字体,而是您在字体文件夹中指定的自定义字体。
As you can see that the text that appeared on the AVD has not a default android font, rather it has the custom font that you specified in the fonts folder.
注意:- 在使用自定义字体时,您需要注意字体的尺寸和支持的字符。
Note − You need to take care of the size and the character supported by the font , when using custom fonts.