1.1 结构
java.lang.Object
android.view.View
android.widget.TextView
直接子类:
Button, CheckedTextView, Chronometer, DigitalClock,EditText
间接子类:
AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText,MultiAutoCompleteTextView, RadioButton, ToggleButton
1.2 API
属性名称 |
描述 |
android:autoLink |
设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all) |
android:autoText |
如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。 |
android:bufferType |
指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符, 也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,参见这里1、这里2。 |
android:capitalize |
设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditText此属性说明。 |
android:cursorVisible |
设定光标为显示/隐藏,默认显示。 |
android:digits |
设置允许输入哪些字符。如“1234567890.+-*/%\n()” |
android:drawableBottom |
在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。 |
android:drawableLeft |
在text的左边输出一个drawable,如图片。 |
android:drawablePadding |
设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 |
android:drawableRight |
在text的右边输出一个drawable,如图片。 |
android:drawableTop |
在text的正上方输出一个drawable,如图片。 |
android:editable |
设置是否可编辑。这里无效果,参见EditView。 |
android:editorExtras |
设置文本的额外的输入数据。在EditView再讨论。 |
android:ellipsize |
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动) |
android:freezesText |
设置保存文本的内容以及光标的位置。参见:这里。 |
android:gravity |
设置文本位置,如设置成“center”,文本将居中显示。 |
android:hint |
Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在EditView中使用,但是这里也可以用。 |
android:imeOptions |
附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditText中再详细说明,此处无用。 |
android:imeActionId |
设置IME动作ID。在EditText再做说明,可以先看这篇帖子:这里。 |
android:imeActionLabel |
设置IME动作标签。在EditText再做说明。 |
android:includeFontPadding |
设置文本是否包含顶部和底部额外空白,默认为true。 |
android:inputMethod |
为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。 |
android:inputType |
设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditText中再详细说明,这里无效果。 |
android:linksClickable |
设置链接是否点击连接,即使设置了autoLink。 |
android:marqueeRepeatLimit |
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 |
android:ems |
设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度,如图: |
android:maxEms |
设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 |
android:minEms |
设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。 |
android:maxLength |
限制显示的文本长度,超出部分不显示。 |
android:lines |
设置文本的行数,设置两行就显示两行,即使第二行没有数据。 |
android:maxLines |
设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 |
android:minLines |
设置文本的最小行数,与lines类似。 |
android:lineSpacingExtra |
设置行间距。 |
android:lineSpacingMultiplier |
设置行间距的倍数。如”1.2” |
android:numeric |
如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EditText将详细说明。 |
android:password |
以小点”.”显示文本 |
android:phoneNumber |
设置为电话号码的输入方式。 |
android:privateImeOptions |
设置输入法选项,此处无用,在EditText将进一步讨论。 |
android:scrollHorizontally |
设置文本超出TextView的宽度的情况下,是否出现横拉条。 |
android:selectAllOnFocus |
如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。EditText中设置后无效果。 |
android:shadowColor |
指定文本阴影的颜色,需要与shadowRadius一起使用。效果: |
android:shadowDx |
设置阴影横向坐标开始位置。 |
android:shadowDy |
设置阴影纵向坐标开始位置。 |
android:shadowRadius |
设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。 |
android:singleLine |
设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行 |
android:text |
设置显示文本. |
android:textAppearance |
设置文字外观。如“?android:attr/textAppearanceLargeInverse ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse |
android:textColor |
设置文本颜色 |
android:textColorHighlight |
被选中文字的底色,默认为蓝色 |
android:textColorHint |
设置提示信息文字的颜色,默认为灰色。与hint一起使用。 |
android:textColorLink |
文字链接的颜色. |
android:textScaleX |
设置文字缩放,默认为1.0f。分别设置0.5f/1.0f/1.5f/2.0f效果如下: |
android:textSize |
设置文字大小,推荐度量单位”sp”,如”15sp” |
android:textStyle |
设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2]可以设置一个或多个,用“|”隔开 |
android:typeface |
设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] |
android:height |
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) |
android:maxHeight |
设置文本区域的最大高度 |
android:minHeight |
设置文本区域的最小高度 |
android:width |
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width的区别看 |
TextView为文本控件,在前面的学习中多次用到,可能读者已经对它的用法有所了解了,这里再单独介绍一下,加深理解,因为TextView是一个非常重要和常用的控件。
我们通常在xml布局文件中声明一个TextView,代码如下:
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
package com.pms.tvdemo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
publicclassMyTextViewextends Activity {
private TextView mTextView;//声明TextView对象
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.tv);//取得TextView对象
mTextView.setText("我的第一个文本");//设置TextView显示的内容
mTextView.setTextColor(Color.GREEN);//设置TextView的颜色
mTextView.setBackgroundColor(Color.WHITE);//设置TextView的背景色
}
}
TextView对象方法及对应的xml属性
方法 |
xml属性 |
setText |
android:text |
setTextSize |
android:textSize |
setTextColor |
android:textColor |
setBackgroundResource |
android:background |
Android中提供了如下常见颜色:
Color.BLACK;
Color.BLUE;
Color.CYAN;
Color.DKGRAY;
Color.GRAY;
Color.GREEN;
Color.LTGRAY;
Color.MAGENTA;
Color.RED;
Color.TRANSPARENT。
除此之外,当文字中出现URL、E-mail、电话号码等的时候,还可以为TextView设置链接。总结起来,一共有4种方法来为TextView实现链接。
(1)在xml里添加android:autoLink属性。如果写为android:autoLink=”all”,则为所有种类添加链接。当然,同样的也可以在Java代码中完成,用法为tv.setAutoLinkMask(Linkify.ALL)。
(2)将显示内容写到资源文件,一般为String.xml中,并且用标签来声明链接,但是这还不够,要激活这个链接,需要在Java代码中使用setMovementMethod()方法设置TextView可点击。
(3)用Html类的fromHtml()方法格式化要放到TextView里的文字。
(4)用Spannable或实现它的类,如SpannableString。与其他方法不同的是,Spannable对象可以为个别字符设置链接(当然也可以为个别字符设置颜色、字体等,实现某些字符高亮显示的效果等)。
看一个实例:
package com.pms.tvdemo;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
publicclass MyTextView extends Activity {
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView t2 = (TextView) findViewById(R.id.tv02);
t2.setMovementMethod(LinkMovementMethod.getInstance()); //设置TextView可点击,第二种方式实现
TextView t3 = (TextView) findViewById(R.id.tv03);
t3.setText(
Html.fromHtml(
"text3: " +
"链接到google " +
"使用HTML在java代码中实现"));
t3.setMovementMethod(LinkMovementMethod.getInstance()); //第三种方式实现
//创建一个 SpannableString对象
SpannableString ss = new SpannableString(
"text4: 点击这里拨打电话,点击这里链接到google");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置0~6字符为粗体
ss.setSpan(new URLSpan("tel:4155551212"), 9, 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //设置9~11字符为拨号链接
ss.setSpan(new URLSpan("http://www.google.com"), 18, 20,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);//设置18~20为网站链接
ss.setSpan(new BackgroundColorSpan(Color.RED), 23 ,29,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置 23~29字符为红色高亮
TextView t4 = (TextView) findViewById(R.id.tv04);
t4.setText(ss);//SpannableString对象设置给TextView
t4.setMovementMethod(LinkMovementMethod.getInstance());//第四种实现方式
}
}
一共定义了4种实现方法,其中第二种方法一定要将文字内容写在资源文件中,写在Java代码中是无法实现的。
main.xml:
xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/first_link"
/>
<TextView
android:id="@+id/tv02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second_link"
/>
<TextView
android:id="@+id/tv03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
LinearLayout>
String.xml:
xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">Hello World, MyTextView!string>
<stringname="app_name">TextView Demostring>
<stringname="first_link"><b>text1:b>这是用第一种方式实现的超链接。你只要点击
string>
<stringname="second_link"><b>text2:b>这是第二种方式实现的超链接,用一个<a>标签来声明。<ahref="http://www.google.com">链接到googlea>
用”tel“就可以链接到拨号<ahref="tel:123456789">拨打电话a>.
string>
resources>
点击http://www.google.com