Android TextView--项目开发少不了你

前言:

转眼间来到12月份了,又差不多过去一年了!时光匆匆,我依然埋头苦干着,失去了很多和朋友互动的时间,哎,心想还是等有钱了再多聚聚吧!现在开始从零学习Android吧,虽然技术迭代很快,但能从中找到快乐的话,即使累点也是值得的。Android项目开发中估计少不了TextView,今天就总结一些常用的方法,方便查找、复习...

 

一、相关属性设置:

 

1.TextView设置中划线:

   textview.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);

 

 

2.TextView设置下划线:

   textview.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

 

 

3.TextView设置取消划线:

   textview.getPaint().setFlags(0);

 

上面介绍的三种TextView加划线的方法是很常用的,但如果像上面那样设置的话 文本会出现不清晰,看起来就不正常了,你们可以试试看;这时就要用到加清晰设置

 

textview.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG); 

 

4.和设置划线的方法一起使用:如

下划线加清晰:

textview.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);

 

5.抗锯齿

textview.getPaint().setAntiAlias(true);

 

6.在代码中设置中文加粗

textview.getPaint().setFakeBoldText(true);

 

7.设置文本可被选中

textview.setTextIsSelectable(true);//xml中 android:textIsSelectable="true"

 

8.设置TextView在获取焦点后选中全部内容

textview.setSelectAllOnFocus(true);//xml中 android:selectAllOnFocus="true"

 

9.设置文本被选中内容的高亮背景色

textview.setHighlightColor(getResources().getColor(R.color.Green));

//xml中 android:textColorHighlight="@color/Green"

 

10.TextView链接相关(如网址、拨打电话等,设置后,点击会打开系统默认程序)

android:autoLink有6个属性值分别为:None、web、email、phone、map、all。

None:默认的,不匹配任何连接。

web:网址。

email:邮箱。

phone:电话号码。

map:匹配映射网址。

all:匹配所有连接。

 

 

 

二、代码实践展示:

xml布局文件:

 

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000"
    android:textSize="20sp"
    android:text="默认字体:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="默认字体默认字体默认字体默认字体"/>

<!--1中划线-->
<TextView
    style="@style/textTitle"
    android:text="1.中划线:"/>
<TextView
    android:id="@+id/tv_center_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="中划线中划线中划线中划线中划线"/>


<!--2下划线-->
<TextView
    style="@style/textTitle"
    android:text="2.下划线:"/>
<TextView
    android:id="@+id/tv_bottom_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="下划线下划线下划线下划线下划线"/>


<!--3取消划线-->
<TextView
    style="@style/textTitle"
    android:text="3.取消划线:"/>
<TextView
    android:id="@+id/tv_cancel_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="取消划线取消划线取消划线取消划线"/>

<!--4加清晰字体下划线-->
<TextView
    style="@style/textTitle"
    android:text="4.加清晰字体下划线:"/>
<TextView
    android:id="@+id/tv_clear_line"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="加清晰划线加清晰划线加清晰划线"/>


<!--5抗锯齿-->
<TextView
    style="@style/textTitle"
    android:text="5.抗锯齿:"/>
<TextView
    android:id="@+id/tv_anti_aliasing"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:text="抗锯齿抗锯齿抗锯齿抗锯齿抗锯齿"/>


<!--6一段文本,多种样式-->
<TextView
    style="@style/textTitle"
    android:text="6.一段文本,多种样式:"/>
<TextView
    android:id="@+id/tv_two_style"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:text="一段文本,多种样式一段文本,多种样式"/>

<!--7在布局文件中加粗-->
<TextView
    style="@style/textTitle"
    android:text="7.在布局文件中设置加粗:"/>
<TextView
    android:id="@+id/bold"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="在布局文件中设置加粗(中文English都可以)"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:textStyle="bold" />

<!--8在代码中设置加粗-->
<TextView
    style="@style/textTitle"
    android:text="8.在代码中设置加粗:"/>
<TextView
    android:id="@+id/bold1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="在代码中设置中文加粗"
    android:textColor="#7b35ae"
    android:textSize="18sp"/>

<!--9在布局文件中设置斜体字-->
<TextView
    style="@style/textTitle"
    android:text="9.在布局文件中设置斜体字:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="在布局文件中设置斜体字在布局文件中设置斜体字"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:textStyle="italic" />

<!--10在布局文件中设置斜体字加粗-->
<TextView
    style="@style/textTitle"
    android:text="10.在布局文件中设置斜体字加粗:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="在布局文件中设置斜体字加粗在布局文件中设置斜体字加粗"
    android:textColor="#7b35ae"
    android:textSize="18sp"
    android:textStyle="italic|bold" />

<!--11设置TextView超出两行用省略号代替-->
<TextView
    style="@style/textTitle"
    android:text="11.设置TextView超出两行用省略号代替:"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textColor="#7b35ae"
    android:ellipsize="end"
    android:lines="2"
    android:text="设置TextView超出两行用省略号代替,设置TextView超出两行用省略号代替,设置TextView超出两行用省略号代替"
    android:textSize="18sp"/>

<!--12设置文本是否可被选中-->
<TextView
    style="@style/textTitle"
    android:text="12.设置文本是否可被选中:"/>
<TextView
    android:id="@+id/tv_selectable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置文本是否可被选中设置文本是否可被选中"
    android:textColor="#7b35ae"
    android:textSize="18sp"/>

<!--13设置TextView在获取焦点后是否选中全部内容-->
<TextView
    style="@style/textTitle"
    android:text="13.设置TextView在获取焦点后是否选中全部内容:"/>
<TextView
    android:id="@+id/tv_selectAllOnFocus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="设置TextView在获取焦点后是否选中全部内容"
    android:textColor="#7b35ae"
    android:textSize="18sp"/>

<!--14.设置TextView被选中内容的高亮背景色-->
<TextView
    style="@style/textTitle"
    android:text="14.设置TextView被选中内容的高亮背景色:"/>
<TextView
    android:id="@+id/tv_colorhighlight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="设置TextView被选中内容的高亮背景色设置TextView被选中内容的高亮背景色"
    android:textColor="#7b35ae"
    android:textSize="18sp"/>

<!--15.设置TextView链接相关内容-->
<TextView
    style="@style/textTitle"
    android:text="15.设置TextView链接相关内容:"/>
<TextView
    android:id="@+id/tv_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="设置TextView链接相关内容,设置TextView链接相关内容"
    android:textColor="#7b35ae"
    android:textSize="18sp"/>

 

 

 

java代码:

 

public class MainActivity extends AppCompatActivity {

    private TextView mTvCenterLine;
    private TextView mTvBottomLine;
    private TextView mTvCancelLine;
    private TextView mTvClearLine;
    private TextView mTvAntiAliasing;
    private TextView mTvTwoStyle;
    private TextView mBold1;
    private TextView mTvSelectable;
    private TextView mTvSelectAllOnFocus;
    private TextView mTvColorhighlight;
    private TextView mTvLink;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initSetting();
    }

    private void initView() {
        mTvCenterLine = (TextView) findViewById(R.id.tv_center_line);
        mTvBottomLine = (TextView) findViewById(R.id.tv_bottom_line);
        mTvCancelLine = (TextView) findViewById(R.id.tv_cancel_line);
        mTvClearLine = (TextView) findViewById(R.id.tv_clear_line);
        mTvAntiAliasing = (TextView) findViewById(R.id.tv_anti_aliasing);
        mTvTwoStyle = (TextView) findViewById(R.id.tv_two_style);
        mBold1 = (TextView) findViewById(R.id.bold1);
        mTvSelectable = (TextView) findViewById(R.id.tv_selectable);
        mTvSelectAllOnFocus = (TextView) findViewById(R.id.tv_selectAllOnFocus);
        mTvColorhighlight = (TextView) findViewById(R.id.tv_colorhighlight);
        mTvLink = (TextView) findViewById(R.id.tv_link);

    }

    private void initSetting() {
        /**中划线*/
        mTvCenterLine.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        /**下划线*/
        mTvBottomLine.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        /**取消划线*/
        mTvCancelLine.getPaint().setFlags(0);
        /**下划线加清晰*/
        mTvClearLine.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        /**抗锯齿*/
        mTvAntiAliasing.getPaint().setAntiAlias(true);
        /**一段文本,多种样式*/
        String txt = "我是一个Textview显示的一段文本,要显示多种样式";
        Spannable textSpan = new SpannableStringBuilder(txt);
        //textSpan.setSpan(TextAppearanceSpan样式,开始位置,结束位置,Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new TextAppearanceSpan(this,R.style.textStyle1)
                , 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new TextAppearanceSpan(this,R.style.textStyle2)
                , 4, 12, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new TextAppearanceSpan(this,R.style.textStyle3)
                , 12, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        textSpan.setSpan(new TextAppearanceSpan(this,R.style.textStyle4)
                , 20, txt.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mTvTwoStyle.setText(textSpan);
        /**在代码中设置中文加粗*/
        mBold1.getPaint().setFakeBoldText(true);
        /**设置文本是否可被选中*/
        mTvSelectable.setTextIsSelectable(true);//xml中 android:textIsSelectable="true"
        /**设置TextView在获取焦点后是否选中全部内容*/
        mTvSelectAllOnFocus.setSelectAllOnFocus(true);//xml中 android:selectAllOnFocus="true"
        /**设置文本被选中内容的高亮背景色*/
        mTvColorhighlight.setHighlightColor(getResources().getColor(R.color.Green));//xml中 android:textColorHighlight="@color/Green"
        /**
         * TextView链接相关
         * android:autoLink属性的介绍:
         None:默认的,不匹配任何连接。
         web:网址。
         email:邮箱。
         phone:电话号码。
         map:匹配映射网址。
         all:匹配所有连接。
         * */
        //自动识别对应的连接,点击出发对应的Android程序
        mTvLink.setAutoLinkMask(Linkify.ALL);//xml中 android:autoLink="all"
        String string = "My-web:http://blog.csdn.net/qq_29269233\n\n"
                +"My-email:zsml2016@163.com\n\n"
                +"My-phone:+86 0755-888888";
        mTvLink.setText(string);
    }
    

}

 

 

 

 

 

三、效果图如下:

 

 

 

四、demo下载地址:https://github.com/zsml2016/TextViewDemo

 

 

 

支持我的话可以关注下我的公众号,也欢迎大家投稿~

扫一扫关注我的微信公众号:程序猿在广东

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值