android.graphics.Typeface实践

来源:互联网 发布:服务器怎么访问数据库 编辑:程序博客网 时间:2024/06/06 04:39
 字体风格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风格的字体:有五种形式



编程实现以上静态域字体


① 创建新工程

② 修改mianActivity.java,实现多种字体TextView显示
  1. package zyf.TypefaceStudy;
  2. /*导入要使用的包*/
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.graphics.Typeface;
  6. import android.os.Bundle;
  7. import android.view.ViewGroup;
  8. import android.widget.LinearLayout;
  9. import android.widget.TextView;
  10. public class TypefaceStudy extends Activity {
  11. /** Called when the activity is first created. */
  12. /*
  13. * android.graphics.Typeface java.lang.Object
  14.         Typeface类指定一个字体的字体和固有风格.
  15. * 该类用于绘制,与可选绘制设置一起使用,
  16.      如textSize, textSkewX, textScaleX 当绘制(测量)时来指定如何显示文本.
  17. */
  18. /* 定义实例化一个 布局大小,用来添加TextView */
  19. final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT;
  20. /* 定义TextView对象 */
  21. private TextView bold_TV, bold_italic_TV, default_TV,
  22.                         default_bold_TV,italic_TV,monospace_TV,
  23.                         normal_TV,sans_serif_TV,serif_TV;
  24. /* 定义LinearLayout布局对象 */
  25. private LinearLayout linearLayout;
  26. /* 定义LinearLayout布局参数对象 */
  27. private LinearLayout.LayoutParams linearLayouttParams;
  28. @Override
  29.     public void onCreate(Bundle icicle) {
  30.         super.onCreate(icicle);
  31.         /* 定义实例化一个LinearLayout对象 */
  32.         linearLayout = new LinearLayout(this);
  33.         /* 设置LinearLayout布局为垂直布局 */
  34.         linearLayout.setOrientation(LinearLayout.VERTICAL);
  35.         /*设置布局背景图*/
  36.         linearLayout.setBackgroundResource(R.drawable.back);
  37.         /* 加载LinearLayout为主屏布局,显示 */
  38.         setContentView(linearLayout);
  39.         /* 定义实例化一个LinearLayout布局参数 */
  40.         linearLayouttParams =
  41.            new LinearLayout.LayoutParams(WRAP_CONTENT,WRAP_CONTENT);
  42.         constructTextView();
  43.         setTextSizeOf();
  44.         setTextViewText() ;
  45.         setStyleOfFont();
  46.         setFontColor();
  47.         toAddTextViewToLayout();
  48.     }
  49.     public void constructTextView() {
  50.         /* 实例化TextView对象 */
  51.         bold_TV = new TextView(this);
  52.         bold_italic_TV = new TextView(this);
  53.         default_TV = new TextView(this);
  54.         default_bold_TV = new TextView(this);
  55.         italic_TV = new TextView(this);
  56.         monospace_TV=new TextView(this);
  57.         normal_TV=new TextView(this);
  58.         sans_serif_TV=new TextView(this);
  59.         serif_TV=new TextView(this);
  60.     }
  61.     public void setTextSizeOf() {
  62.         // 设置绘制的文本大小,该值必须大于0
  63.         bold_TV.setTextSize(24.0f);
  64.         bold_italic_TV.setTextSize(24.0f);
  65.         default_TV.setTextSize(24.0f);
  66.         default_bold_TV.setTextSize(24.0f);
  67.         italic_TV.setTextSize(24.0f);
  68.         monospace_TV.setTextSize(24.0f);
  69.         normal_TV.setTextSize(24.0f);
  70.         sans_serif_TV.setTextSize(24.0f);
  71.         serif_TV.setTextSize(24.0f);
  72.     }
  73.     public void setTextViewText() {
  74.         /* 设置文本 */
  75.         bold_TV.setText("BOLD");
  76.         bold_italic_TV.setText("BOLD_ITALIC");
  77.         default_TV.setText("DEFAULT");
  78.         default_bold_TV.setText("DEFAULT_BOLD");
  79.         italic_TV.setText("ITALIC");
  80.         monospace_TV.setText("MONOSPACE");
  81.         normal_TV.setText("NORMAL");
  82.         sans_serif_TV.setText("SANS_SERIF");
  83.         serif_TV.setText("SERIF");
  84.     }
  85.     public void setStyleOfFont() {
  86.         /* 设置字体风格 */
  87.         bold_TV.setTypeface(null, Typeface.BOLD);
  88.         bold_italic_TV.setTypeface(null, Typeface.BOLD_ITALIC);
  89.         default_TV.setTypeface(Typeface.DEFAULT);
  90.         default_bold_TV.setTypeface(Typeface.DEFAULT_BOLD);
  91.         italic_TV.setTypeface(null, Typeface.ITALIC);
  92.         monospace_TV.setTypeface(Typeface.MONOSPACE);
  93.         normal_TV.setTypeface(null, Typeface.NORMAL);
  94.         sans_serif_TV.setTypeface(Typeface.SANS_SERIF);
  95.         serif_TV.setTypeface(Typeface.SERIF);
  96.     }
  97.     public void setFontColor() {
  98.         /* 设置文本颜色 */
  99.         bold_TV.setTextColor(Color.BLACK);
  100.         bold_italic_TV.setTextColor(Color.CYAN);
  101.         default_TV.setTextColor(Color.GREEN);
  102.         default_bold_TV.setTextColor(Color.MAGENTA);
  103.         italic_TV.setTextColor(Color.RED);
  104.         monospace_TV.setTextColor(Color.WHITE);
  105.         normal_TV.setTextColor(Color.YELLOW);
  106.         sans_serif_TV.setTextColor(Color.GRAY);
  107.         serif_TV.setTextColor(Color.LTGRAY);
  108.     }
  109.     public void toAddTextViewToLayout() {
  110.         /* 把TextView加入LinearLayout布局中 */
  111.         linearLayout.addView(bold_TV, linearLayouttParams);
  112.         linearLayout.addView(bold_italic_TV, linearLayouttParams);
  113.         linearLayout.addView(default_TV, linearLayouttParams);
  114.         linearLayout.addView(default_bold_TV, linearLayouttParams);
  115.         linearLayout.addView(italic_TV, linearLayouttParams);
  116.         linearLayout.addView(monospace_TV, linearLayouttParams);
  117.         linearLayout.addView(normal_TV, linearLayouttParams);
  118.         linearLayout.addView(sans_serif_TV, linearLayouttParams);
  119.         linearLayout.addView(serif_TV, linearLayouttParams);
  120.     }
  121. }
复制代码
③ 结果
 



原创粉丝点击