Android SDK 对应的平台下面有相应的字体,能够了解一下:android
XXX\android-sdk-windows\platforms\android-X\data\fonts windows
DroidSans.ttf:英文正常显示字体
DroidSans-Bold.ttf:英文正常显示字体(粗体)
DroidSerif-Italic.ttf:英文正常显示字体(斜体)
DroidSansMono.ttf:英文正常显示字体(等宽体)ide
DroidSerif-Bold.ttf:英文备用显示字体(粗体)
DroidSerif-BoldItalic.ttf:英文备用显示字体(粗斜体)
DroidSerif-Regular.ttf:英文备用显示字体(印刷体)布局
DroidSansFallback.ttf:中文正常显示字体字体
.....spa
Android提供三种“sans”(default),“serif”,“monospace”字体供选择,在XML布局文件中,能够使用typeface属性进行设置,可是有时候提供的字体并不能知足咱们的需求,好比有关时钟,计时器的应用时,咱们就但愿使用液晶数字字体,使界面更加美观,这时候能够经过自定义字体:orm
我没有去研究Android中使用的ttf文件是否和Windows中是否通用,从Windows系统中拷贝的字体是否能正确显示。xml
拿到ttf文件后,拷贝到项目下assets文件夹中,能够新建文件夹,注意加载资源时路径正确就ok了。blog
关键代码:ip
- TextView textViewTime = (TextView) findViewById(R.id.textViewTime);
- // Load font resource and set typeface.
- Typeface lcdFont = Typeface.createFromAsset(getAssets(), "xxx.ttf");
- textViewTime.setTypeface(lcdFont);
我这里作了一下改进,显示当前时间:
对应的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:gravity="center">
- <TextView android:text="TextView" android:id="@+id/textViewTime"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:layout_gravity="center" android:textSize="45sp" ></TextView>
- </LinearLayout>
完整代码:
- /**
- * @author Brant
- * @decription
- */
- public class Main extends Activity {
- private Thread mWorkThread;
- private final static SimpleDateFormat TIMESTAMP_Format = new SimpleDateFormat(
- "HH:mm:ss");// 时间戳格式
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- final
- TextView textViewTime = (TextView) findViewById(R.id.textViewTime);
- // Load font resource and set typeface.
- Typeface lcdFont = Typeface.createFromAsset(getAssets(), "digifaw.ttf");
- textViewTime.setTypeface(lcdFont);
- mWorkThread = new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textViewTime.setText(TIMESTAMP_Format
- .format(new Date(System.currentTimeMillis())));
- }
- });
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- });
- mWorkThread.start();
- }
- @Override
- public boolean onKeyUp(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_BACK:
- mWorkThread.interrupt();
- finish();
- return true;
- }
- return super.onKeyUp(keyCode, event);
- }
- }
效果截图:
附件是相应的字体文件。