API 23 widget.TextView——属性分析

本文详细分析了Android API 23中TextView的属性,包括:文本选择、输入法特性、图像处理、布局控制、样式和颜色等方面,旨在帮助开发者更好地理解和使用TextView。

参阅http://android.xsoftlab.net/reference/android/widget/TextView.html

 

public class

TextView

extends View

implements ViewTreeObserver.OnPreDrawListener  

Added in API level 1

 

 

java.lang.Object

   ↳ android.view.View

     ↳ android.widget.TextView

 

已知直接子类

AppCompatTextView

Button

CheckedTextView

Chronometer

DigitalClock

EditText

RowHeaderView

TextClock

 

【概述】

TextView用于向用户显示文本,并可选择允许编辑它。

TextView是一个完整的文本编辑器,但是基本类配置为不允许编辑;其子类EditText可对文本进行编辑。

设置XML属性android:textIsSelectable=true或者调用setTextIsSelectable(true),可允许用户对TextView的文本内容进行复制/粘贴操作。

备注:

使用<TextView>来输入文本通常是一个错误,您应该使用<EditText>EditTextTextView的一个子类,一些编辑支持由TextView提供,因此可以在TextView上设置一些与输入相关的属性。 但是,使用TextView和输入属性通常是剪切和粘贴错误。 要输入文本,您应该使用<EditText>。 这个检查也检查TextView的子类,如ButtonCheckBox,因为它们有相同的问题:它们不应该与可编辑的属性一起使用。

 

【总结】

【嵌套类】

枚举TextView.BufferType    API1

接口TextView.OnEditorActionListenerAPI3

TextView.SavedStateAPI1

 

XML属性】

android:autoLink

API 1

关联方法:setAutoLinkMask(int mask)

属性说明: 设置是否显示为超链接(none/web/email/phone/map/all) 默认为none

可以是以下常量之一:

none 不匹配。默认值。

web  匹配URL地址,单击使用浏览器打开

email匹配邮箱地址,单击发送邮件

phone匹配电话号码,单击拨打电话

map  匹配地图地址,单击使用google地图打开,支持英文

all  匹配上述四种格式

它对应的全局资源符号是autoLink

 

TextView的内容既包含汉字又包含网址。如要想成功实践超链接功能,就必须在汉字后添加空格或者换行,否则地址就是此TextView的所有内容。

 

HTML解析超链接:通过Spanned对象设置text,而通过HTMLformHTML来获取Spanned对象

textView.setText(Html.fromHtml("百度:<a href='http://www.baidu.com/'>http://www.baidu.com</a>

<h1><i><font color='#000FFF'>h1号字 斜体 蓝色</font></i>"));

 

android:autoText

API 1

关联方法:setKeyListener(KeyListener input)

属性说明: 如果true,指定此TextView有一个文本输入法,并自动纠正一些常见的拼写错误。默认false

可以是一个布尔值(boolean),true”或“false”。

可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。

它对应的全局资源符号是autoText

 

全局属性autoTextAPI 3被弃用,使用inputType代替。

AndroidautoText不应使用在TextView,应使用在EditView

 

android:breakStrategy

API 23

关联方法:setBreakStrategy(int breakStrategy)

属性说明:打破策略(控制段落布局)。

可以是以下常量之一:

simple          0

high_quality   1

balanced       2段落长度平衡

它对应的全局资源符号是breakStrategy



android:bufferType

API 1

关联方法:setText(CharSequence,TextView.BufferType)

属性说明:指定getText()方式取得的文本类别。确定getText()将返回的最小类型。

可以是以下常量之一:

normal    默认。

editable  类似于StringBuilder可追加字符,getText后可调用append方法设置文本内容。

spannable 则可在给定的字符区域使用样式

它对应的全局资源符号是bufferType

 

有意思的是 Editable继承了Spannable所以具备较多的功能。

注意,EditTextLogTextBox总是返回Editable,即使你在这里指定的功能更强大。

 

android:capitalize

API 1

关联方法:setKeyListener(KeyListener input)

属性说明:设置英文字母大写类型

可以是以下常量之一:

none       不转换默认值。

sentences  每一个句子首字母大写

words      每一个单词首字母大写

characters 每一个英文字母都大写

它对应的全局资源符号是bufferType

 

全局属性capitalizeAPI 3被弃用,使用inputType代替。

Androidcapitalize不应使用在TextView,应使用在EditView

 

android:cursorVisible

API 1

关联方法:setCursorVisible(boolean visible)

属性说明:设定光标为显示/隐藏,默认显示。如果设置false,即使选中了也不显示光标栏。

可以是一个布尔值(boolean),true”或“false”。

还可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。

它对应的全局资源符号是cursorVisible

 

AndroidcursorVisible不应使用在TextView,应使用在EditView

设置EditText光标在右边,输入内容从右往左。Androidgravity=right/end

 

android:digits

API 1

关联方法:setKeyListener(KeyListener input)

属性说明:设置允许输入哪些字符。如1234567890.+-*/% ()0123456789xyzXYZ。常用于密码验证。

可以是一个字符串值(string),使用\\;”等转义字符,“\\n”或“\\uxxxxUnicode字符。

还可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。

它对应的全局资源符号是digits

 

Androiddigits不应使用在TextView,应使用在EditView

 

android:drawablePadding

API 1

关联方法:setCompoundDrawablePadding(int pad)

属性说明:设置文本框内文本与图像之间的间距,与drawableLeftdrawableRightdrawableTopdrawableBottom一起使用,可设置为负数,单独使用没有效果。

可以是一个带有单位的浮点型尺寸值(float),如:14.5sp”。有效的单位包括:px(像素)dp(密度无关的像素)sp(基于引用字体的尺寸来缩放的像素)in(英寸)mm(毫米)

可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。

它对应的全局资源符号是drawablePadding

 

android:drawableTop

API 1

关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)

属性说明:在文本框顶部绘制指定图像。

android:drawableBottom

API 1

关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)

属性说明:在文本框底部绘制指定图像。

android:drawableLeft

API 1

关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)

属性说明:在文本框左边绘制指定图像。

android:drawableRight

API 1

关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)

属性说明:在文本框右边绘制指定图像。

android:drawableStart

API 14

关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int start,int top,int end,int bottom)

属性说明:在文本框开始处绘制指定图像。

android:drawableEnd

API 14

关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int start,int top,int end,int bottom)

属性说明:在文本框结尾处绘制指定图像。

 

可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。

可以是一个颜色值(color),格式#rgb”、”#argb”、”#rrggbb”、”#aarrggbb”。

它对应的全局资源符号是drawableTopdrawableBottomdrawableLeftdrawableRightdrawableStartdrawableEnd

 

setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)                 API 1

setCompoundDrawablesRelative(Drawablestart,Drawable top,Drawableend,Drawable bottom)                 API 17

Drawable必须已经调用了setBounds(Rect)

setCompoundDrawablesWithIntrinsicBounds(Drawable left,Drawable top,Drawable right,Drawable bottom) API 1

setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)                                API 3

setCompoundDrawablesRelativeWithIntrinsicBounds(Drawablestart,Drawable top,Drawableend,Drawable bottom) API 17

setCompoundDrawablesRelativeWithIntrinsicBounds(intstart

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值