在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. PHP zip压缩文件及解压

    PHP zip压缩文件及解压 利用ZipArchive 类实现 只有有函数.界面大家自己写 ZipArchive(PHP 5.3 + 已自带不需要安装dll) /** * 文件解压 * @param ...

  2. not valid for Running the scheme

    The run destination iPhone6 Plus is not valid for Running the scheme 'MyApp’. Phone6 Plus's iOS 8.0 ...

  3. [solaris]odbc-oracle,简单测试

    #include <string> #include <iostream> #include <stdio.h> #include <sql.h> #i ...

  4. logstash 根据type 判断输出

    # 更多ELK资料请访问 http://devops.taobao.com 一.配置前需要注意: 1.Use chmod to modify nginx log file privilege. E.g ...

  5. 10-3[RF] feature selection

    main idea: 计算每一个feature的重要性,选取重要性前k的feature: 衡量一个feature重要的方式:如果一个feature重要,则在这个feature上加上noise,会对最后 ...

  6. U-boot中实现Yaffs2+HwEcc

    经过老手的指点,要实现Yaffs2+HwEcc,重点在于chip->ops.mode由MTD_OOB_RAW到MTD_OOB_AUTO.经过几天的筹备,今天要对其下手了.为了真实展现分析移植过程 ...

  7. #include <sstream>

    1 std::istringstream 2 std::stringstream 1 std::istringstream input 1 在一个字符串string里提取部分数据,这些数据以空格' ' ...

  8. iOS学习,需要懂的一些基础

    1.  KVC 与 KVO 全称是Key-value coding,翻译成键值编码.顾名思义,在某种程度上跟map的关系匪浅.它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制. 全称 ...

  9. C#中dynamic的正确用法【转】

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  10. HashMap 的遍历key与value的方法

    HashMap最经常使用的使用方法是依据key增删改查value,有时候会须要知道一个HashMap有多少个keys以及这些keys都是什么,能够用以下的代码实现. 方法1: Map map = ne ...