Think4Android(一)之TextView

来源:互联网 发布:知伯忠诚 编辑:程序博客网 时间:2024/05/22 13:11

       首先还是上效果图:


       安卓开发当中TextView是最常用的组件之一了,今天对TextView的思考主要目的是为了在开发中能够注意到一些细节从而达到给用户更佳的操作体验。比如说有时候需要更改我们TextView的字体、有时候需要一个单行的滚动的提示、有时候需要带有滚动条的TextView、又有时候我们的TextView需要任性的style。


-Think For TextView
 --1.TextView 如何去更改字体
 --2.TextView 超链接
 --3.TextView 单行条幅滚动效果
 --4.TextView 不同文本效果

1.更改字体

//TextView 更改字体方式 1://将字体文件保存在assets/fonts/目录下,创建Typeface对象 注:assets文件夹在项目根目录下创建Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/font1.ttf");//使用字体tvTip1.setTypeface(typeFace);//TextView 更改字体方式 2(推荐方式)://如果我们想对全局TextView设置字体,则可以自己写一个MyTextView来继承我们的TextView//这种方式使用起来非常方便,并且可以让我们更方便的来修改我们的Textview的字体tvTip2.setTypefaceName("font2");

继承自TextView的MyTextView

package com.xiaoping.view;import android.content.Context;import android.graphics.Typeface;import android.util.AttributeSet;import android.widget.TextView;public class MyTextView extends TextView {private Context mContext;//这里给我们要使用的字体赋一个默认值,即全局需要设置的字体private String TypefaceName = "font1";public String getTypefaceName() {return TypefaceName;}/** * 对外提供修改默认字体的方法 * @param typefaceName */public void setTypefaceName(String typefaceName) {TypefaceName = typefaceName;Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/" + TypefaceName + ".ttf");this.setTypeface(typeface);System.gc();}/** * 重写TextView的构造方法 并为他设置上字体 * @param context * @param attrs * @param defStyle */public MyTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);this.mContext = context;if (TypefaceName != null && !"".equals(TypefaceName)) {Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");this.setTypeface(typeface);}}public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;if (TypefaceName != null && !"".equals(TypefaceName)) {Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");this.setTypeface(typeface);}}public MyTextView(Context context) {super(context);this.mContext = context;if (TypefaceName != null && !"".equals(TypefaceName)) {Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");this.setTypeface(typeface);}}}


2.在TextView中显示超链接的方法

    <TextView        android:id="@+id/tv_tip3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:autoLink="all"        android:paddingTop="@dimen/activity_vertical_margin"        android:text="百度一下: www.baidu.com 客服电话: 010-59928888 邮箱:mbaidu@baidu.com"        android:textSize="18sp" />
设置上autoLink属性即可

     <!-- android:autoLink="web"-设置自动识别链接,值web为匹配Web网址 -->

     <!--android:autoLink="phone"-设置自动识别链接,值phone为匹配电话号码 -->

     <!--android:autoLink="email"-设置自动识别链接,值email为匹配Email地址 -->

     <!--android:autoLink="all"-设置自动识别链接,值all为匹配所有 -->

3.单行条幅滚动效果

    <TextView        android:id="@+id/tv_tip4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:paddingTop="@dimen/activity_vertical_margin"        android:singleLine="true"android:ellipsize="marquee"android:focusable="true"android:marqueeRepeatLimit="marquee_forever"android:focusableInTouchMode="true"        android:text="非运营商免费通话被叫停?工信部:未经许可皆非法。"        android:textSize="18sp"        android:drawableLeft="@drawable/msg" />
需要设置的是

android:singleLine="true" //单行显示
android:ellipsize="marquee"//自动滚动

android:focusable="true"//获取焦点
android:marqueeRepeatLimit="marquee_forever"//持续滚动
android:focusableInTouchMode="true"//触摸获取焦点


4.不同文本效果

//通过Html方式也可以实现单个TextView显示不同效果的文本//tvTip6.setText(Html.fromHtml("<font color=\'#858585\' size = \'6\'>2016</font><font color=\'#f02387\' size = \'3\'>年</font>"));    SpannableStringBuilder builder = new SpannableStringBuilder(tvTip6.getText().toString());    //准备好需要用到的不同前景色    ForegroundColorSpan blue = new ForegroundColorSpan(Color.BLUE);    ForegroundColorSpan red = new ForegroundColorSpan(Color.RED);    ForegroundColorSpan yellow = new ForegroundColorSpan(Color.YELLOW);    //改变字体颜色为蓝色    builder.setSpan(blue,0,4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    builder.setSpan(red,5,7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    builder.setSpan(yellow,8,10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    //改变字体大小    builder.setSpan(new AbsoluteSizeSpan(80),0,4,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    builder.setSpan(new AbsoluteSizeSpan(80),5,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    builder.setSpan(new AbsoluteSizeSpan(80),8,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);    tvTip6.setText(builder);

关于TextView这肯定只是冰山一角,也肯定还有很多东西值得我们去探索

源码下载


0 0
原创粉丝点击