1.TextView 主要是在Android中实现文字说明等功能:
1.显示丰富的文本(URL、字体大小、颜色)
在TextView中预定了类似Html的标签,通过标签可以是TextView控件显得不同颜色、大小、字体的文字。
<font> : 设置颜色和字体
<big>:设置大号
<small>:设置小号
<i> <b>:斜体、粗体
<a>:链接地址
<img>:插入图片
使用Html.fromHtml方法将这些标签的字符串转成Charsequenece对象,然后再设置给TextView。
如果想在显示的文本中将URL地址、邮箱地址、电话产生超链接的效果可以使用android:autoLink设置。
autoLink ----> 1.None 不匹配任何链接(default)
2.web 网址
3.email 邮箱
4.phone 电话号码
5.map 匹配映射网址
6.all 匹配所有链接
例子代码;
MainActivity.java
package com.example.androiduitextview;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private TextView textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
}
private void initComponent() {
// TODO Auto-generated method stub
textView = (TextView) this.findViewById(R.id.textView);
String html1 = new String();
html1+="<font color='red'>I love android application</font><br>";
html1+="<font color='#0000ff'><big> i am bigest</big></font><p>";
html1+="<big><a href='http://www.baidu.com'>百 度</a></big>";
CharSequence content = Html.fromHtml(html1);
textView.setText(content);
textView.setMovementMethod(LinkMovementMethod.getInstance());// 点击时,跳转至链接
textView2 = (TextView) this.findViewById(R.id.textView2);
StringBuilder html2 = new StringBuilder();
html2.append("My URL: http://www.baidu.com\n");
html2.append("My Email: 547333138@qq.com\n");
html2.append("My Phone: 12345678901");
textView2.setText(html2.toString());
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="20sp" />
<!-- android:text="@string/link_text" -->
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="20sp" />
</LinearLayout>
效果图: