Android 设置字体

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 {

	/*
	* 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.onCreate(icicle);
		/* 定义实例化一个LinearLayout对象*/
		linearLayout = new LinearLayout(this);
		/* 设置LinearLayout布局为垂直布局*/
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		/*设置布局背景图*/
		linearLayout.setBackgroundResource(R.drawable.back);
		/* 加载LinearLayout为主屏布局,显示*/
		setContentView(linearLayout);
		
		/* 定义实例化一个LinearLayout布局参数*/
		linearLayouttParams =
		new LinearLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
		constructTextView();
		setTextSizeOf();
		setTextViewText() ;
		setStyleOfFont();
		setFontColor();
		toAddTextViewToLayout();
	} 
	public void constructTextView() {
		/* 实例化TextView对象*/
		bold_TV = new TextView(this);
		bold_italic_TV = new TextView(this);
		default_TV = new TextView(this);
		default_bold_TV = new TextView(this);
		italic_TV = new TextView(this);
		monospace_TV=new TextView(this);
		normal_TV=new TextView(this);
		sans_serif_TV=new TextView(this);
		serif_TV=new TextView(this);
	} 
	public void setTextSizeOf() {
		// 设置绘制的文本大小,该值必须大于0
		bold_TV.setTextSize(24.0f);
		bold_italic_TV.setTextSize(24.0f);
		default_TV.setTextSize(24.0f);
		default_bold_TV.setTextSize(24.0f);
		italic_TV.setTextSize(24.0f);
		monospace_TV.setTextSize(24.0f);
		normal_TV.setTextSize(24.0f);
		sans_serif_TV.setTextSize(24.0f);
		serif_TV.setTextSize(24.0f);
	} 
	public void setTextViewText() {
		/* 设置文本*/
		bold_TV.setText("BOLD");
		bold_italic_TV.setText("BOLD_ITALIC");
		default_TV.setText("DEFAULT");
		default_bold_TV.setText("DEFAULT_BOLD");
		italic_TV.setText("ITALIC");
		monospace_TV.setText("MONOSPACE");
		normal_TV.setText("NORMAL");
		sans_serif_TV.setText("SANS_SERIF");
		serif_TV.setText("SERIF");
	} 
	public void setStyleOfFont() {
		/* 设置字体风格*/
		bold_TV.setTypeface(null, Typeface.BOLD);
		bold_italic_TV.setTypeface(null, Typeface.BOLD_ITALIC);
		default_TV.setTypeface(Typeface.DEFAULT);
		default_bold_TV.setTypeface(Typeface.DEFAULT_BOLD);
		italic_TV.setTypeface(null, Typeface.ITALIC);
		monospace_TV.setTypeface(Typeface.MONOSPACE);
		normal_TV.setTypeface(null, Typeface.NORMAL);
		sans_serif_TV.setTypeface(Typeface.SANS_SERIF);
		serif_TV.setTypeface(Typeface.SERIF);
	} 
	public void setFontColor() {
		/* 设置文本颜色*/
		bold_TV.setTextColor(Color.BLACK);
		bold_italic_TV.setTextColor(Color.CYAN);
		default_TV.setTextColor(Color.GREEN);
		default_bold_TV.setTextColor(Color.MAGENTA);
		italic_TV.setTextColor(Color.RED);
		monospace_TV.setTextColor(Color.WHITE);
		normal_TV.setTextColor(Color.YELLOW);
		sans_serif_TV.setTextColor(Color.GRAY);
		serif_TV.setTextColor(Color.LTGRAY);
	} 
	public void toAddTextViewToLayout() {
		/* 把TextView加入LinearLayout布局中*/
		linearLayout.addView(bold_TV, linearLayouttParams);
		linearLayout.addView(bold_italic_TV, linearLayouttParams);
		linearLayout.addView(default_TV, linearLayouttParams);
		linearLayout.addView(default_bold_TV, linearLayouttParams);
		linearLayout.addView(italic_TV, linearLayouttParams);
		linearLayout.addView(monospace_TV, linearLayouttParams);
		linearLayout.addView(normal_TV, linearLayouttParams);
		linearLayout.addView(sans_serif_TV, linearLayouttParams);
		linearLayout.addView(serif_TV, linearLayouttParams);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值