Android应用中使用自定义文字
在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应用中使用自定义文字的更多相关文章
- Android XML中引用自定义内部类view的四个why
今天碰到了在XML中应用以内部类形式定义的自定义view,结果遇到了一些坑.虽然通过看了一些前辈写的文章解决了这个问题,但是我看到的几篇都没有完整说清楚why,于是决定做这个总结. 使用自定义内部类v ...
- Android TextView 中实现部分文字变色以及点击事件
首先要想实现文字变色以及点击,都需要使用到SpannableStringBuilder,实例化该类也很简单,只需将你想要处理的字符串当做参数 SpannableStringBuilder spanna ...
- Android开发中 .9.png格式图形设计:
Android .9.png设计 宿舍大神在做android项目,有幸得知.9.png的图形格式. 不知道大家是否注意过聊天气泡和锁屏时随着你文字的增多和你的滑动而跟着变化并且分辨率没有变低的图形?是 ...
- Android中制作自定义dialog对话框的实例
http://www.jb51.net/article/83319.htm 这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...
- Android系统在新进程中启动自定义服务过程(startService)的原理分析
在编写Android应用程序时,我们一般将一些计算型的逻辑放在一个独立的进程来处理,这样主进程仍然可以流畅地响应界面事件,提高用户体验.Android系统为我们提供了一个Service类,我们可以实现 ...
- android中通过自定义xml实现你需要的shape效果 xml属性配置
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决.不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的 ...
- Android自定义View研究--View中的原点坐标和XML中布局自定义View时View触摸原点问题
这里只做个汇总~.~独一无二 文章出处:http://blog.csdn.net/djy1992/article/details/9715047 Android自定义View研究--View中的原点坐 ...
- Android中的自定义注解(反射实现-运行时注解)
预备知识: Java注解基础 Java反射原理 Java动态代理 一.布局文件的注解 我们在Android开发的时候,总是会写到setContentView方法,为了避免每次都写重复的代码,我们需要使 ...
- Android Studio中如何使用自定义的framework库
在安卓app开发中,通常不会遇到需要使用自定义framework库的情况,使用的都是标准的内核库.但也有例外,比如针对定制化的ROM,ROM厂商可能在ROM中对安卓源码做过修改,对应用层app暴露出与 ...
随机推荐
- poj 1450 Gridland
Gridland Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- Twitter License for Android
1.Apache Software Foundation Apache 软件基金会 2.Apache Thrift 跨平台传输数据,Thrift与其他传输方式的比较: xml与JSON相比体积太 ...
- android玩耍(-) adbshell安装
一 什么是adbshell http://adbshell.com/ Android Debug Bridge (adb) is a command line tool that lets you c ...
- linux系统下安全管理
1.引导程序安全 linux系统的root密码是很容易破解的,当然前提是你没有设置引导程序密码,如GRUB或LILO,为了防止通过引导程序破译root密码,强烈建 议设置GRUB或LILO的引导密码, ...
- servlet上传图片 服务器路径(转)
1.在servlet中上传图片,上传的文件夹是imge在webroot下,主要代码如下 private void saveImage(HttpServletRequest request, HttpS ...
- opennebula extend(expending) auth module ldap
LDAP Authentication addon permits users to have the same credentials as in LDAP, so effectively cent ...
- Euro Efficiency(完全背包)
Euro Efficiency Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Tot ...
- vs快捷键
Ctrl+E,D ----格式化全部代码 Ctrl+A+K+FCtrl+E,F ----格式化选中的代码 Ctrl+K+FCTRL + SHIFT + B生成解决方案 Alt+B+B 或 F6 生成当 ...
- VS中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
最近使用VS,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示.查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型” ...