Android为TextView添加字体库和设置描边的方法

  

  

开发安卓的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView的字体属性支持,sans serif和等宽字体这三种字体,如果在没有指定字体的情况下,系统会使用无作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。

  

<强> 1。在XML文件中设置

        & lt; !——使用默认的无字体——比;   & lt; TextView   android: id=癅 + id/无”=android:文本“Hello, World”   android: textSize=" 20 sp”   android:字体="无"/比;      & lt; !——使用默认的衬线字体——比;   & lt; TextView   android: id=癅 + id/衬线”=android:文本“Hello, World”   android: textSize=" 20 sp”   android:字体=俺摹?比;      & lt; !——使用默认的等宽字体字体——比;   & lt; TextView   android: id=癅 + id/等宽字体”=android:文本“Hello, World”   android: textSize=" 20 sp”   android:字体="等宽字体"/比;      之前      

<强> 2。在Java代码中设置

  

第一步:获取TextView实例

     //获取textView实例   TextView TextView=findViewById (R.id.textview);      

第二步:设置字体

     //设置衬线字体   textView.setTypeface (Typeface.SERIF);//设置无字体   textView.setTypeface (Typeface.SANS_SERIF);//设置等宽字体字体   textView.setTypeface (Typeface.MONOSPACE);   之前      

  

Android系统自带有对字体的设置,这些设置是对字体的显示方式的设置,比如加粗,倾斜,下划线,字号等,但是并没有提供对于字体类型的徐选择,比如设置成楷体,隶书或雅黑等.Android系统只固定默认一种字体类型,所以如果开发人员需要修改字体类型,那么就必须需自己引入字体库。

  

<强> 1。引入字体库的实现

  

第一步:在资产目录下新建字体目录,并把ttf字体文件放到该目录下。

  

第二步:在Java代码中实现

     //实例化TextView   TextView TextView=findViewById (R.id.textview);//得到AssetManager   AssetManager经理=getAssets ();//根据路径得到字体   字体tf=字体。createFromAsset(下,“字体/pocknum.ttf”);//设置字体   textView.setTypeface (tf);   之前      

2。引入字体库后的效果图

  

 Android为TextView添加字体库和设置描边的方法

  

  

Android的默认控件TextView,相信大家都不会陌生,但是原生的TextView是不支持描边效果的,但是在实际的开发过程中,经常会遇到为TextView添加描边的需求,因此就要对原生的TextView进行拓展,使其支持自定义内部和外部颜色的描边TextView。描边效果的实现原理其实很简单,无非就是获取到TextPaint类,先进行一次比默认大小的文字内容稍微大一点的绘制,然后再进行一次默认大小的文字内容的绘制,然后通过属性设置两种不同的颜色,这样就产生出了描边效果。

  

为TextView添加描边,要用到TextPaint的几个属性:

        TextPaint油漆=outlineTextView.getPaint ();//实例化TextPaint对象   paint.setStrokeWidth (15);//设置描边的宽度   paint.setStyle (Paint.Style.STROKE);//设置画笔属性为描边   strokeTextView.setTextColor (Color.parseColor (# 000000));//设置描边的颜色(不能与文本颜色一致)      

其中strokeTextView为自定义TextView的实例,代码如下:

  

1。在构造函数中添加

        公开课StrokeTextView延伸TextView {   私人TextView outlineTextView=零;      公共StrokeTextView(上下文语境){   超级(上下文);      outlineTextView=new TextView(上下文);   init ();   }      公共StrokeTextView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);      attrs outlineTextView=new TextView(上下文);   init ();   }      公共StrokeTextView(上下文语境、AttributeSet attrs int defStyle) {   超级(上下文、attrs defStyle);      outlineTextView=new TextView(上下文、attrs defStyle);   init ();   }      公共空间init () {   TextPaint油漆=outlineTextView.getPaint ();   paint.setStrokeWidth (3);//描边宽度   paint.setStyle (Style.STROKE);   outlineTextView.setTextColor (Color.parseColor (# 000000));//描边颜色   outlineTextView.setGravity (getGravity ());   }      @Override   公共空间setLayoutParams (ViewGroup。LayoutParams params) {   super.setLayoutParams (params);   outlineTextView.setLayoutParams (params);   }      @Override   保护空白>   公开课StrokeTextView延伸TextView {      私人TextView outlineTextView=零;   私人TextPaint strokePaint;      公共StrokeTextView(上下文语境){   超级(上下文);      outlineTextView=new TextView(上下文);   }      公共StrokeTextView(上下文语境,AttributeSet attrs) {   超级(上下文,attrs);      attrs outlineTextView=new TextView(上下文);   }      公共StrokeTextView(上下文语境、AttributeSet attrs int defStyle) {   超级(上下文、attrs defStyle);      outlineTextView=new TextView(上下文、attrs defStyle);   }      @Override   公共空间setLayoutParams (ViewGroup。LayoutParams params) {   super.setLayoutParams (params);   outlineTextView.setLayoutParams (params);   }      @Override   保护无效onMeasure (int widthMeasureSpec int heightMeasureSpec) {   超级。onMeasure (widthMeasureSpec heightMeasureSpec);      AssetManager经理=context.getAssets ();   字符串路径="字体/new_text.ttf”;   字体类型=字体。createFromAsset(经理、路径);//设置轮廓文字   CharSequence进行outlineText=outlineTextView.getText ();   如果(outlineText==null | | ! outlineText.equals (this.getText ())) {   outlineTextView.setText (getText ());   outlineTextView.setTypeface(类型);   setTypeface(类型);   postInvalidate ();   }   outlineTextView。测量(widthMeasureSpec heightMeasureSpec);   }      @Override   保护无效onLayout(布尔改变,int, int, int, int底部){   超级。onLayout(改变,左,上,右,下);   outlineTextView。布局(左,上,右,下);   }      @Override   保护无效onDraw(帆布画布){   AssetManager经理=context.getAssets ();   字符串路径="字体/new_text.ttf”;   字体类型=字体。createFromAsset(经理、路径);      如果(strokePaint==null) {   strokePaint=new TextPaint ();   }//复制原来TextViewg画笔中的一些参数   TextPaint油漆=getPaint ();   strokePaint.setTextSize (paint.getTextSize ());   strokePaint.setTypeface(类型);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android为TextView添加字体库和设置描边的方法