相信大家必定都知道TextView主要是在android中实现文字说明等功能,那么TextView能否显示更加丰富的文本,如URL、字体大小、颜色这些,答案是肯定的!

那么究竟如何来让我们的TextView显示的内容更加丰富多彩呢,这一篇来跟大家一起探讨一下:

首先呢,大家要知道的是,我们网页里能够显示多样性的文字,都归功与HMTL标签对吧,其实在Android的TextView中也预定了一些类似HTML的标签,通过标签可以使TextView控件显示不同颜色、大小、字体的文字:

<font>:设置颜色和字体

<big>:设置大号

<small>:设置小号

<i>\<b>:分别是斜体和粗体

<a>:链接地址

<img>:插入图片

使用这些标签可以用Html.fromHtml方法将这些标签的字符串转换成Charsequence对象,然后再TextView中进行设置;

如果想在显示的文本中将URL地址、邮箱地址、电话产生超链接的效果,可以使用android:autoLink来设置,该属性的规则如下:

属性值 描述

None 不匹配任何链接(默认)

web 网址

email 邮箱

phone 电话号码

map 匹配映射网址

all 匹配所有链接


下面通过一个实例来讲解一下如何灵活使用这些标签:

实现的丰富文字TextView截图:

Android之UI学习篇一:TextVeiw如何显示丰富的文本_第1张图片


点击"百度"文字链接之后,打开了浏览器并跳转到了百度网址,截图如下:

Android之UI学习篇一:TextVeiw如何显示丰富的文本_第2张图片


点击电话号码"+86148998943"之后弹出了拨打电话的界面,截图:

Android之UI学习篇一:TextVeiw如何显示丰富的文本_第3张图片


代码如下:

TextViewActivity.java

public class Android_TextviewActivity extends Activity {private TextView textView1;private TextView textView2;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        /*         * 实现在TextView中显示URL、不同大小、字体、颜色的文本         * */        textView1=(TextView)findViewById(R.id.textview1);        textView2=(TextView)findViewById(R.id.textview2);        //添加一段html的标示        String html="<font color='red'>I love Android</font><br>";        html+="<font color='#00ff00'><big><i> I love Android </i></big></font><p>";        html+="<big><a href='http://www.baidu.com'>百度</a></big>";        CharSequence charSequence=Html.fromHtml(html);        textView1.setText(charSequence);        //点击的时候产生超链接        textView1.setMovementMethod(LinkMovementMethod.getInstance());        String text="我的URL:http://www.sina.com\n";//这里的\n是换行符        text+="我的Email:[email protected]\n";        text+="我的电话:+86148998933";        textView2.setText(text);        textView2.setMovementMethod(LinkMovementMethod.getInstance());    }    }

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/textview1"        android:padding="20sp"/>         <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/textview2"        android:padding="20sp"        android:autoLink="all"        android:textSize="20sp"        android:text="@string/link_text"/></LinearLayout>


更多相关文章

  1. Android—文字轮播
  2. Android自由选择TextView的文字
  3. Android 添加图片水印(图片+文字)
  4. activity 标签总结1
  5. Android TextView文字过多时,通过滚动条显示多余内容
  6. 安卓文字过长,跑马灯文字滚动效果
  7. Android 实现多行文本跑马灯效果
  8. Android 中如何获取editText文本信息
  9. android常用控件应用之文本框(TextView)特效

随机推荐

  1. Android之getSystemService
  2. Android中通过getSystemService取得服务
  3. Android开发学习之设置Android壁纸的功能
  4. android中遇到问题总结
  5. android中的Handler(2)
  6. Android(安卓)音频管理器AudioManager类
  7. Android如何保存和读取设置
  8. Android(安卓)增量更新实例(Smart App Upd
  9. Android(安卓)ContentProvider实现应用间
  10. android的文件系统结构及其引导过程的初