Android API之Typeface代码演示

Typeface详细规定了字体的字型和固有特性。当绘制(和量测)时,使用画笔尤其是在任意使用像textSize, textSkewX, textScaleX用来指定文字显示,这是很有帮助的。

演示范例(为个人原创,当然遇见问题时参考了一些国内/国外的一些范例):

1)创建布局Layout

//创建线性布局

        LinearLayout linearLayout=new LinearLayout(this);     

        //设定线性布局为垂直方向

        linearLayout.setOrientation(LinearLayout.VERTICAL);

        //以该线性布局做视图

        setContentView(linearLayout);

2)针对正常字体

        //普通正常字体

        normal=new TextView(this);      

        //设置字体内容,请注意:目前Android主要针对拉丁语系可使用字型设定,中文暂不支持

        normal.setText("Normal Font FYI");      

        //设置字体大小

        normal.setTextSize(20.0f);

        //设置字型为默认,正常字体

        normal.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);

        //增加该字体并显示到布局linearLayout

        linearLayout.addView(normal, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

       

3)针对粗体字体 

       //粗体字体

        bold=new TextView(this);

        bold.setText("Bold Font FYI");

        bold.setTextSize(20.0f);

        //设置字体颜色为蓝色

        bold.setTextColor(Color.BLUE);      

       //设置字型为默认粗体,粗体字体

        bold.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);

        linearLayout.addView(bold, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


4)针对斜体字体

         //斜体字体

        italic=new TextView(this);

        italic.setTextSize(20f);

        italic.setText("Italic Font FYI");      

       //设置字体颜色为红色

        italic.setTextColor(Color.RED);

        //设置字型为等宽字型,斜体字体

        italic.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);

        linearLayout.addView(italic, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

       

5)针对粗斜体字体 

       //粗斜体字体

        italic_bold=new TextView(this);

        italic_bold.setTextSize(20f);

        italic_bold.setText("Italic & Bold Font FYI");

        //设置字体颜色为黄色

        italic_bold.setTextColor(Color.YELLOW); 

        //设置字型为等宽字型,斜体字体

        italic_bold.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);

        linearLayout.addView(italic_bold, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 

6)针对中文仿“粗体”

        //针对Android字型的中文字体问题

        chinese=new TextView(this);

        chinese.setText("中文粗体显示效果");      

        //设置字体颜色

        chinese.setTextColor(Color.MAGENTA);

        chinese.setTextSize(20.0f);

        chinese.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);

        //使用TextPaint的仿粗体设置setFakeBoldTexttrue。目前还无法支持仿斜体方法

        tp = chinese.getPaint();

        tp.setFakeBoldText(true);

        linearLayout.addView(chinese, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

 

7 自定义创建字型

      //自定义字体字型

        custom=new TextView(this);

        //字体MgOpenCosmeticaBold.ttf放置于assets/font/路径下

        typeface=Typeface.createFromAsset(getAssets(),"font/MgOpenCosmeticaBold.ttf");

        custom.setTypeface(typeface);

        custom.setText("Custom Font FYI");

        custom.setTextSize(20.0f); 

        //设置字体颜色

        custom.setTextColor(Color.CYAN);

        linearLayout.addView(custom, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


效果截图:

Android API之Typeface代码演示
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值