在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. linux下socket编程-UDP

    下面是UDP的服务器的代码: /* server.c */ #include <stdio.h> #include <string.h> #include <netine ...

  2. html 中head显示 在标题栏里面的图片

    在<head>标签里加<link rel="Shortcut Icon" href="你的ico图片地址" /> 一般标题栏里的图片是1 ...

  3. 使用微信 SDK 上传图片到七牛

    总体思路是:在微信下选好图片后将图片上传到微信服务器,在后端使用微信服务器返回的图片 serverId 加上调用接口的 ApiTicket 通过七牛的 fetch 接口向微信服务器下载多媒体文件的接口 ...

  4. fastjson的常用使用方法

    package Demo; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import ...

  5. Django模板-在视图中使用模板

    之前我们已经有了自己的视图mysite.views.py中,应该是这样子的 from django.http import HttpResponse import datetime def curre ...

  6. “如何稀释scroll事件”引出的问题

    背景:我在segmentfault提了个问题如何稀释onscroll事件,问题如下: 面试时问到这个问题,是这样的:    面试官问一个关于滚动到某个位置的时候出现一个顶部的导航栏,答完之后,她接着问 ...

  7. MVC 创建带图片的<A></A>标签

    <a href="@Url.Action("Detail", "Product", new { messageId = item.message ...

  8. Oracle EBS-SQL (WIP-5):检查非标任务本身选上了MRP净值.sql

    SELECT WE.WIP_ENTITY_NAME,            MSI.SEGMENT1,            MSI.DESCRIPTION,            WDJ.CLASS ...

  9. mac下配置java环境

    1.tomcat配置 http://www.cnblogs.com/freeyiyi1993/p/3436368.html 2.下载eclipse和jdk安装  jdk去oracle网站下载

  10. Delphi SysErrorMessage 函数和系统错误信息表

    在看 API 文档时, 我们经常见到 GetLastError; 它可以返回操作后系统给的提示. 但 GetLastError 返回的只是一个信息代码, 如何返回对应的具体信息呢? FormatMes ...