更改与显示文字标签

更改与显示文字标签

       在项目中新建一个TextViewActivity继承Activity的类并实现onCreate()方法。后创建一个textview.xml的布局文件。在布局文件中设置默认的根布局为LinearLayout,并添加一个TextView组件。

<TextView

        android:id="@+id/TextView_tv01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:shadowColor="#FFFF00"

        android:shadowDx="1"

        android:shadowDy="1"

        android:shadowRadius="1"

        android:textColor="#888277"

        android:textSize="24sp"/>

       在TextView中,设置了id为TextView_tv01,字体颜色为#888277、大小为24sp,阴影颜色为黄色(#FFFF00)、阴影大小为1,偏移为1。

       在TextViewActivity类中首先通过FindViewById()得到TextView的实例对象。

this.tv = (TextView)super.findViewById(R.id.TextView_tv01);

而后通过TextView的setText()方法设置TextView显示的文字。

this.tv.setText("Hello Android");

图片:


这里的setText()方法被重载了多次。

setText(CharSequence text)

setText(CharSequence text, BufferType type)

setText(char[] text, int start, int len)

setText(int resid)

setText(int resid, BufferType type)

 

这里的BufferType是个enum的枚举类,里面保存了3个数值:NORMAL,SPANNABLE, EDITABLE

 

注意在Activity中,如果设置的Text设置的内容为HTML TAG。

如:

this.tv.setText("<a href = \"http://www.baidu.com\">百度</a>");

运行效果:


如果真的要在TextView中实现HTML的超链接,可以通过Html类的fromHtml()方法来实现。但是TextView中如果要实现可以点击的话,还需要先设置TextView的setMovementMethod()方法,里面传入LinkMovementMethod.getInstance()。如:

this.tv.setMovementMethod(LinkMovementMethod.getInstance());

this.tv.setText(Html.fromHtml("<a href = \"http://www.baidu.com\">百度</a>"));



   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值