{ black_TV.setTextColor(Color. BLACK); blue_TV.setTextColor(Color. BLUE);
dkgray_TV.setTextColor(Color. DKGRAY ); gray_TV.setTextColor(Color. GRAY);
green_TV.setTextColor(Color. GREEN); ltgray_TV.setTextColor(Color. LTGRAY );
magenta_TV.setTextColor(Color. MAGENTA);
4
Android 编程基础
red_TV.setTextColor(Color. RED); transparent_TV.setTextColor(Color.
TRANSPARENT ); white_TV.setTextColor(Color. WHITE); yellow_TV.setTextColor(Color.
YELLOW ); } /* 构造实例化 TextView
对象
*/ public void constructTextView() { this
black_TV = new TextView(this this); this blue_TV = new TextView(this this); this
cyan_TV = new TextView(this this); this dkgray_TV = new TextView(this this); this
gray_TV = new TextView(this this); this green_TV = new TextView(this this); this
ltgray_TV = new TextView(this this); this magenta_TV = new TextView(this this);
this red_TV = new TextView(this this); new TextView(this this transparent_TV =
this); this white_TV = new TextView(this this); this yellow_TV = new TextView(this
this); } /* 把 TextView 添加到 LinearLayout
布局中
*/ public void addTextView()
{ myLayout.addView(black_TV, layoutP); myLayout.addView(blue_TV, layoutP);
myLayout.addView(cyan_TV, layoutP); myLayout.addView(dkgray_TV, layoutP);
myLayout.addView(gray_TV, layoutP); myLayout.addView(green_TV, layoutP);
myLayout.addView(ltgray_TV, layoutP); myLayout.addView(magenta_TV, layoutP);
myLayout.addView(red_TV, layoutP); myLayout.addView(transparent_TV, layoutP);
myLayout.addView(white_TV, layoutP); myLayout.addView(yellow_TV, layoutP); } }
5
Android 编程基础
③ 结果
6
Android
编程基础
android.graphics.Typeface 实践
字体风格
Typeface 种类
int Style
类型
BOLD BOLD_ITALIC ITALIC NORMAL Typeface
类型
DEFAULT DEFAULT_BOLD
MONOSPACE SANS_SERIF SERIF
粗体 粗斜体 斜体 普通字体
默认字体 默认粗体 单间隔字体 无衬线字体 衬线字体
Typeface.create(Typeface family,int style)
创建一个混合型新的字体:
有
4*5 中搭配
Typeface.setTypeface (Typeface tf, int style)
设置一个混合型字体:
有
4*5 中搭配
Typeface.setTypeface(Typeface tf)
设置一个只有
Typeface 风格的字体:有五种形式
7
Android 编程基础
编程实现以上静态域字体
① 创建新工程
②
修改
mianActivity.java,
实现多种字体
TextView
显示
package zyf.TypefaceStudy; /*导
入要使用的包
*/ import android.app.Activity; import android.graphics.Color; import
android.graphics.Typeface; import android.os.Bundle; import
android.view.ViewGroup; import android.widget.LinearLayout; import
android.widget.TextView; public class TypefaceStudy extends Activity { /** Called
when the activity is first created. */ /* * android.graphics.Typeface
java.lang.Object Typeface 类指定一个字体的字体和固有风格. * 该类用于绘制,与可选绘制设置一起使
用
, 如 textSize, textSkewX, textScaleX 当绘制(测量)时来指定如何显示文本. */ /* 定义实例化一
个 布局大小
,用来添加 TextView */ final int WRAP_CONTENT = ViewGroup.LayoutParams.
WRAP_CONTENT; /* 定义 TextView
对象
*/ private TextView bold_TV, bold_italic_TV,
default_TV, default_bold_TV,italic_TV,monospace_TV,
normal_TV,sans_serif_TV,serif_TV; /* 定义 LinearLayout
布局对象
*/ private
LinearLayout linearLayout; /* 定义 LinearLayout
布局参数对象
*/ private
LinearLayout.LayoutParams linearLayouttParams; @Override public void
onCreate(Bundle icicle) { super super.onCreate(icicle); /* 定义实例化一个
LinearLayout
对象
*/ this linearLayout = new LinearLayout(this this); /* 设置