在Android系统中可以很方便的修改字体样式。系统提供了三种样式,分别是sans,serif,monospace。可以在xml布局文件中通过

android:typeface="[sans,serif,monospace]"  

当然Android也为开发者提供了使用外部字体样式的方式。在官方给出的Design帮助文档中有一个roboto的字体库。字体的一些效果如下:

使用方式:

首先在assets目录下,新建一份fonts目录。将想要使用的字体文件(*.ttf)文件拷贝进来。

其次编写如下代码:

Typeface tf1 = Typeface.createFromAsset(getAssets(),
"fonts/xxx.ttf");
TextView tv1 = new TextView(this);
tv1.setTypeface(tf1);
tv1.setText("0123");
tv1.setTextSize(20);

这里给出一个完整的实例:

xml文件定义:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:typeface="normal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:typeface="sans" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:typeface="serif" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:typeface="monospace" /> </LinearLayout>

java代码部分:

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView; import com.example.androidtest.R; public class TypeFaceDemo extends Activity { private LinearLayout llRoot; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_typeface);
llRoot = (LinearLayout) this.findViewById(R.id.ll_root); Typeface tf1 = Typeface.createFromAsset(getAssets(),
"fonts/digitalfont.ttf");
TextView tv1 = new TextView(this);
tv1.setTypeface(tf1);
tv1.setText("数字时钟效果:0123");
tv1.setTextSize(20); Typeface tf2 = Typeface.createFromAsset(getAssets(), "fonts/bold.ttf");
TextView tv2 = new TextView(this);
tv2.setTypeface(tf2);
tv2.setText("abcde");
tv2.setTextSize(20); Typeface tf3 = Typeface.createFromAsset(getAssets(),
"fonts/roboto_regular.ttf");
TextView tv3 = new TextView(this);
tv3.setTypeface(tf3);
tv3.setText("ABCD");
tv3.setTextSize(20); Typeface tf4 = Typeface.createFromAsset(getAssets(),
"fonts/simkai.ttf");
TextView tv4 = new TextView(this);
tv4.setTypeface(tf4);
tv4.setText("上面的几种对只能针对英文字符,对中文不起作用。所以从xp系统中拷贝了楷体字体。不过xp系统的字体文件有3m左右大小");
tv4.setTextSize(20); llRoot.addView(tv1);
llRoot.addView(tv2);
llRoot.addView(tv3);
llRoot.addView(tv4);
}
}

要注意的是:Android提供的roboto字体库只对英文起作用。

要注意的是:Android提供的roboto字体库只对英文起作用。

Android应用中使用自定义文字的更多相关文章

  1. Android XML中引用自定义内部类view的四个why

    今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...

  2. Android TextView 中实现部分文字变色以及点击事件

    首先要想实现文字变色以及点击,都需要使用到SpannableStringBuilder,实例化该类也很简单,只需将你想要处理的字符串当做参数 SpannableStringBuilder spanna ...

  3. Android开发中 .9.png格式图形设计:

    Android .9.png设计 宿舍大神在做android项目,有幸得知.9.png的图形格式. 不知道大家是否注意过聊天气泡和锁屏时随着你文字的增多和你的滑动而跟着变化并且分辨率没有变低的图形?是 ...

  4. Android中制作自定义dialog对话框的实例

    http://www.jb51.net/article/83319.htm   这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...

  5. Android系统在新进程中启动自定义服务过程(startService)的原理分析

    在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...

  6. android中通过自定义xml实现你需要的shape效果 xml属性配置

    在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的 ...

  7. Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题

    这里只做个汇总~.~独一无二 文章出处:http://blog.csdn.net/djy1992/article/details/9715047 Android自定义View研究--View中的原点坐 ...

  8. Android中的自定义注解(反射实现-运行时注解)

    预备知识: Java注解基础 Java反射原理 Java动态代理 一.布局文件的注解 我们在Android开发的时候,总是会写到setContentView方法,为了避免每次都写重复的代码,我们需要使 ...

  9. Android Studio中如何使用自定义的framework库

    在安卓app开发中,通常不会遇到需要使用自定义framework库的情况,使用的都是标准的内核库.但也有例外,比如针对定制化的ROM,ROM厂商可能在ROM中对安卓源码做过修改,对应用层app暴露出与 ...

随机推荐

  1. poj3254Corn Fields题解

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9623   Accepted: 5092 Descr ...

  2. Android 圆形背景shape定义

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  3. PHP面试题之算法解析

    面试中经常被问到会什么算法,这里整合一些常见的算法及它们的实现原理.下面的例子都是经过测试可用的,如果有什么问题请告知!! 本人小白,如果有更好的实现方式,敬请赐教,感激不尽!!!! 冒泡排序,快速排 ...

  4. Stack的实现

    public class MyStack<AnyType> { private AnyType [] theItems; private final int DEFAULT_CAPACIT ...

  5. Web学习

    http://book.2cto.com/201309/31936.html http://alvinalexander.com/ 查看锁表进程SQL语句1: select sess.sid,     ...

  6. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

  7. egret-android-support-gradle版

    从3.1.3开始,Egret已经实现了Gradle构建!所以下文你爱看不看! 迟钝的Egret从3.1.3版本才开始支持Gradle,而笔者早在1.6.x版本就已经支持了,说明什么?说明Egret在某 ...

  8. ubuntu qt X11开发环境

  9. wchar_t 、UTF-8、UTF-16的转换方法 - luketty的专栏 - 博客频道 - CSDN.NET

    wchar_t .UTF-8.UTF-16的转换方法 - luketty的专栏 - 博客频道 - CSDN.NET wchar_t .UTF-8.UTF-16的转换方法

  10. web中使用扫描枪

    扫描枪实际上就是一输入设备,只不过它每次在输入的内容后面添加一个回车.因此在web中可以使用js监听回车事件.处理代码如下: jQuery(function() {        jQuery(doc ...