更改与显示文字标签
在项目中新建一个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>"));