java.lang.Object
| android.view.View
| android.widget.TextView
2.直接子类
Button, CheckedTextView, Chronometer, DigitalClock, EditText, TextClock
3.间接子类
AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText, MultiAutoCompleteTextView, RadioButton, Switch, ToggleButton
4.XML属性
android:autoLink
关联方法:setAutoLinkMask(int)
属性说明:设置是否当文本为URL链接email电话号码map时,文本显示为可点击的链接
可设置的值为Linkify - ALL - ;EMAIL_ADDRESSES - 邮箱;
MAP_ADDRESSES - 街道地址; PHONE_NUMBERS - 电话号码;
WEB_URLS - 网络地址
android:autoText
关联方法:setKeyListener(KeyListener)
属性说明:如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。
android:bufferType
关联方法:setText(CharSequence,TextView.BufferType)
属性说明:指定getText()方式取得的文本类别
TextView.BufferType:EDITABLE - 可追加字符;NORMAL - 正常;SPANNABLE -在给定的字符区域使用样式 ;
例子:
//设置textView1为TextView.BufferType.NORMAL
textView1.setText(textView1.getText(),TextView.BufferType.NORMAL);
textView1.append(" Append from textView1");
//设置textView2为TextView.BufferType.EDITABLE
textView2.setText(textView2.getText(), TextView.BufferType.EDITABLE);
textView2.append(" Append from textView2");
//设置textVIew3中Text内容的起始和终止需处理字符的索引位置
int start=(textView3.getText()).toString().indexOf('i');
int end=(textView3.getText()).toString().indexOf('V');
//设置textView3为TextView.BufferType.SPANNABLE
textView3.setText(R.string.textView3, TextView.BufferType.SPANNABLE);
Spannable span=(Spannable)textView3.getText();
span.setSpan(newBackgroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(newStyleSpan(Typeface.BOLD_ITALIC),start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
android:capitalize
关联方法:setKeyListener(KeyListener)
属性说明:设置英文字母大写类型,此处无效果,需要弹出输入法才能看得到;
android:cursorVisible
关联方法:setCursorVisible(boolean)
属性说明:设定光标为显示隐藏,默认显示
android:digits
关联方法:setKeyListener(KeyListener)
属性说明:设置允许输入哪些字符。如“1234567890.+-*%\n()”
android:drawableBottom
关联方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
属性说明:在text的下方输出一个drawable,如图片。
如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者
android:drawableEnd
关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
属性说明:
android:drawableLeft
关联方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
属性说明:在text的左边输出一个drawable,如图片
android:drawablePadding
关联方法:setCompoundDrawablePadding(int)
属性说明:
android:drawableRight
关联方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
属性说明:在text的右边输出一个drawable,如图片
android:drawableStart
关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
属性说明:
android:drawableTop
关联方法:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
属性说明:在text的正上方输出一个drawable,如图片
android:editable
关联方法:
属性说明:设置是否可编辑;参见EditView;
android:editorExtras
关联方法:setInputExtras(int)
属性说明:设置文本的额外的输入数据;参见EditView;
android:ellipsize
关联方法:setEllipsize(TextUtils.TruncateAt)
属性说明:设置当文字过长时,该控件该如何显示;
TextUtils.TruncateAt END - 省略号显示在结尾;MARQUEE - 以跑马灯的方式显示(动画横向移动);
MIDDLE - 省略号显示在中间; START - 省略号显示在开头;
android:ems
关联方法:setEms(int)
属性说明:设置TextView的宽度为N个字符的宽度
android:maxEms
关联方法:setMaxEms(int)
属性说明:设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:minEms
关联方法:setMinEms(int)
属性说明:设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。
android:fontFamily
关联方法:setTypeface(Typeface)
属性说明:
android:freezesText
关联方法:setFreezesText(boolean)
属性说明:设置保存文本的内容以及光标的位置
android:gravity
关联方法:setGravity(int)
属性说明:设置文本位置
android:height
关联方法:setHeight(int)
属性说明:设置文本区域的高度,支持度量单位:px(像素)dpspinmm(毫米)
android:maxHeight
关联方法:setMaxHeight(int)
属性说明:设置文本区域的最大高度
android:minHeight
关联方法:setMinHeight(int)
属性说明:设置文本区域的最低高度
android:hint
关联方法:setHint(int)
属性说明:android:hint:Text为空时显示的文字提示信息,
可通过textColorHint设置提示信息的颜色
android:imeActionId
关联方法:setImeActionLabel(CharSequence,int)
属性说明:设置IME动作ID,此处无用详见EditText.
android:imeActionLabel
关联方法:setImeActionLabel(CharSequence,int)
属性说明:设置IME动作标签,此处无用详见EditText.
android:imeOptions
关联方法:setImeOptions(int)
属性说明:附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角
将显示一个“完成”,而不设置默认是一个回车符号。这个在EditText中再详细说明,
此处无用。
android:includeFontPadding
关联方法:setIncludeFontPadding(boolean)
属性说明:设置文本是否包含顶部和底部额外空白,默认为true。
android:inputMethod
关联方法:setKeyListener(KeyListener)
属性说明:为文本指定输入法,需要完全限定名(完整的包名)。
例如:com.google.android.inputmethod.pinyin,此处无用详见EditText.
android:inputType
关联方法:setRawInputType(int)
属性说明:设置文本的类型,用于帮助输入法显示合适的键盘类型,此处无用详见EditText.
android:lineSpacingExtra
关联方法:setLineSpacing(float,float)
属性说明:设置行间距
android:lineSpacingMultiplier
关联方法:setLineSpacing(float,float)
属性说明:设置行间距的倍数
android:lines
关联方法:setLines(int)
属性说明:设置文本的行数
android:maxLines
关联方法:setMaxLines(int)
属性说明:设置文本的最大行数
android:minLines
关联方法:setMinLines(int)
属性说明:设置文本的最少行数
android:linksClickable
关联方法:setLinksClickable(boolean)
android:marqueeRepeatLimit
关联方法:setMarqueeRepeatLimit(int)
属性说明:在ellipsize指定marquee的情况下,设置重复滚动的次数,
当设置为marquee_forever(value = -1)时表示无限次。
android:maxLength
关联方法:setFilters(InputFilter)
属性说明:限制显示的文本长度,超出部分不显示。
android:numeric
关联方法:setKeyListener(KeyListener)
属性说明:如果被设置,该TextView有一个数字输入法。此处无用,
设置后唯一效果是TextView有点击效果
android:password
关联方法:setTransformationMethod(TransformationMethod)
属性说明:以小点”.”显示文本
例子
android:phoneNumber
关联方法:setKeyListener(KeyListener)
属性说明:设置为电话号码的输入方式
android:privateImeOptions
关联方法:setPrivateImeOptions(String)
属性说明:设置输入法选项,此处无用
android:scrollHorizontally
关联方法:setHorizontallyScrolling(boolean)
属性说明:设置文本超出TextView的宽度的情况下,是否出现横拉条
android:selectAllOnFocus
关联方法:setSelectAllOnFocus(boolean)
属性说明:如果文本是可选择的,让他获取焦点而不是将光标移动为
文本的开始位置或者末尾位置。EditText中设置后无效果。
android:shadowColor
关联方法:setShadowLayer(float,float,float,int)
属性说明:指定文本阴影的颜色,需要与shadowRadius一起使用
android:shadowDx
关联方法:setShadowLayer(float,float,float,int)
属性说明:设置阴影横向坐标开始位置
android:shadowDy
关联方法:setShadowLayer(float,float,float,int)
属性说明:设置阴影纵向坐标开始位置
android:shadowRadius
关联方法:setShadowLayer(float,float,float,int)
属性说明:置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好
android:singleLine
关联方法:setTransformationMethod(TransformationMethod)
属性说明:设置单行显示。如果和layout_width一起使用,
当文本不能全部显示时,后面用“…”来表示。
android:text
关联方法:setText(CharSequence,TextView.BufferType)
属性说明:设置显示文本
android:textAllCaps
关联方法:setAllCaps(boolean)
属性说明:
android:textAppearance
关联方法:
属性说明:设置文字外观。如“?android:attrtextAppearanceLargeInverse”
textAppearanceButton
textAppearanceInverse
textAppearanceLarge
textAppearanceLargeInverse
textAppearanceMedium
textAppearanceMediumInverse
textAppearanceSmall
textAppearanceSmallInverse
android:textColor
关联方法:setTextColor(int)
属性说明:设置文本颜色
android:textColorHighlight
关联方法:setHighlightColor(int)
属性说明:被选中文字的底色,默认为蓝色
android:textColorHint
关联方法:setHintTextColor(int)
属性说明:设置提示信息文字的颜色,默认为灰色。与hint一起使用。
android:textColorLink
关联方法:setLinkTextColor(int)
属性说明:文字链接的颜色
android:textIsSelectable
关联方法:isTextSelectable()
属性说明:文本是否可以被选中
android:textScaleX
关联方法:setTextScaleX(float)
属性说明:设置文字之间间隔,默认为1.0
android:textSize
关联方法:setTextSize(int,float)
属性说明:设置文字大小,推荐度量单位”sp”
android:textStyle
关联方法:setTypeface(Typeface)
属性说明:设置字形[bold(粗体) 0, italic(斜体) 1,
bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开设置文本字体
Typeface:BOLD、BOLD_ITALIC、ITALIC、NORMAL
android:typeface
关联方法:setTypeface(Typeface)
属性说明:设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]
Typeface:DEFAULT(normal)、DEFAULT_BOLD、MONOSPACE、SANS_SERIF、SERIF
android:width
关联方法:setWidth(int)
属性说明:设置文本区域的宽度
android:maxWidth
关联方法:setMaxWidth(int)
属性说明:设置文本区域的最大宽度
android:minWidth
关联方法:setMinWidth(int)
属性说明:设置文本区域的最小宽度
4.部分属性实例
1.autoLink
使用关联函数设置
<span style="font-size:14px;"> // 设置全部文本为链接
tv_1.setAutoLinkMask(Linkify.ALL);
tv_2.setAutoLinkMask(Linkify.ALL);
// 设置网络地址为链接
tv_3.setAutoLinkMask(Linkify.WEB_URLS);
// 设置街道地址为链接
tv_4.setAutoLinkMask(Linkify.MAP_ADDRESSES);
// 设置电话号码为链接
tv_5.setAutoLinkMask(Linkify.PHONE_NUMBERS);
// 设置Email为链接
tv_6.setAutoLinkMask(Linkify.EMAIL_ADDRESSES);
// 设置文本为链接时字体的颜色,默认蓝色
tv_1.setLinkTextColor(Color.RED);
tv_1.setText("0532110");
tv_2.setText("www.baidu.com");
tv_3.setText("www.baidu.com");
tv_4.setText("Room 201, No. 34, Lane 125, XiKang Road(South), HongKou District ");
tv_5.setText("15045104833");
tv_6.setText("3349@163.com");</span>
效果图
2. android:bufferType
<span style="font-size:14px;"> // 设置获取文本类型为正常
tv_1.setText(tv_1.getText(), BufferType.NORMAL);
tv_1.append(" This is the appended text_1");
// 设置获取文本类型为正常
tv_2.setText(tv_2.getText(), BufferType.EDITABLE);
tv_2.append(" This is the appended text_2");
//设置文本在给定的字符区域使用样式
tv_3.setText(tv_3.getText(), TextView.BufferType.SPANNABLE);
Spannable span=(Spannable)tv_3.getText();
span.setSpan(new BackgroundColorSpan(Color.BLUE), 10, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),10, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);</span>
效果图:
1.BufferType.SPANNABLE 确实改变了字符区域的字体
2.EDITABLE之于NORMAL的差异,主要是说EDITABLE之后可以使用textview的append方法。但是实际情况效果是tv_1和tv_2使用append的方是一样的。查看了下源码,确实对BufferType做了处理,但是效果还是一样,很不解。
<span style="font-size:14px;"> if (type == BufferType.EDITABLE || getKeyListener() != null ||
needEditableForNotification) {
createEditorIfNeeded();
mEditor.forgetUndoRedo();
Editable t = mEditableFactory.newEditable(text);
text = t;
setFilters(t, mFilters);
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) imm.restartInput(this);
} else if (type == BufferType.SPANNABLE || mMovement != null) {
text = mSpannableFactory.newSpannable(text);
} else if (!(text instanceof CharWrapper)) {
text = TextUtils.stringOrSpannedString(text);
}</span>
3.android:drawableBottom、android:drawableTop、android:drawableRight、android:drawableLeft、android:drawableStart、android:drawableEnd
XML布局:
<span style="font-size:14px;"> <TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableTop" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableBottom" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableLeft" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableRight" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableStart" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_launcher"
android:gravity="center"
android:text="drawableEnd" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" /></span>
效果图:android:drawableStart、android:drawableEnd这两个属性是在API 17才添加上的,使用时注意版本。
4.android:ellipsize:设置文本超过文本宽度后的显示
XML布局
<span style="font-size:14px;"> <TextView
android:id="@+id/tv_1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:ellipsize="none"
android:gravity="center"
android:lines="1"
android:text="要得到就必须要付出!!!!!"
android:textColor="@android:color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:ellipsize="start"
android:gravity="center"
android:lines="1"
android:text="要得到就必须要付出!!!!!"
android:textColor="@android:color/white"
android:width="96sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_3"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:ellipsize="end"
android:gravity="center"
android:lines="1"
android:text="要得到就必须要付出!!!!!"
android:textColor="@android:color/white"
android:width="96sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_4"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:ellipsize="middle"
android:gravity="center"
android:lines="1"
android:text="要得到就必须要付出!!!!!"
android:textColor="@android:color/white"
android:width="96sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="@color/colorAccent" />
<TextView
android:id="@+id/tv_5"
android:layout_width="120dp"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="@color/colorAccent"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:lines="1"
android:text="要得到就必须要付出!哼唱接撒砥砺风节雷锋精神!"
android:focusable="true"
android:focusableInTouchMode="true"
android:textColor="@android:color/white" /></span>
效果图
1、官方文档介绍,当line设置为2以上时,android:ellipsize只能设置end或者MARQUEE
2、测试跑马灯的方式显示时,有以下情况
2.1 TextView获取焦点时,才会以跑马灯的方式显示,所以需要设置 android:focusable="true"; android:focusableInTouchMode="true"
2.2 在xml文件里设置 android:scrollHorizontally="true",TextView并不能够一跑马灯的形式显示。但是动态设置setHorizontallyScrolling(true);即可。官方文档说,android:scrollHorizontally属性和setHorizontallyScrolling(true)是关联的,实现效果确实不一样,很迷茫。
日积月累,总会进步!