android 控件 文本框 TextView

来源:互联网 发布:网络咨询医生工资待遇 编辑:程序博客网 时间:2024/05/16 01:07
1、继承关系和子类:

java.lang.Object

  ↳android.view.View
↳android.widget.TextView
Known Direct Subclasses
Button, CheckedTextView, Chronometer, DigitalClock, EditText, TextClock
Known Indirect Subclasses
AutoCompleteTextView, CheckBox, CompoundButton, ExtractEditText, MultiAutoCompleteTextView, RadioButton, Switch, ToggleButton

2、定义:

Displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see EditText for a subclass that configures the text view for editing。

简单的说就是用来展示文字信息的控件。

3、XML重要属性:

跑马灯效果:  

android:ellipsize 设置当文字过长时,该控件该如何显示。

可选项“start/end/middle/marquee”来分别设置省略号的出现位置,当设置为marquee时,可设置跑马灯效果。同时需设置以下几项(缺一不可)
android:singleLine="true"   // 设置文本在一行显示 
android:focusable="true"   // 设置是否可以获取焦点 
android:ellipsize="marquee"   //设置文本以marquee方式显示 
android:marqueeRepeatLimit="marquee_forever"   //设置滚动次数,marquee_forever 为无限次 
android:focusableInTouchMode="true"   // 设置是否在触摸模式下获得焦点 
android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all) 
android:autoText 如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。  
android:capitalize 设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。 
android:cursorVisible 设定光标为显示/隐藏,默认显示。 
android:digits 设置允许输入哪些字符。如“1234567890.+-*/% ()” 
android:drawableBottom 在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。 
android:drawableLef t在text的左边输出一个drawable,如图片。 
android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 
android:drawableRight 在text的右边输出一个drawable。 
android:drawableTop 在text的正上方输出一个drawable。 
android:editable 设置是否可编辑。 
android:gravity 设置文本位置,如设置成“center”,文本将居中显示。 
android:hint Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在 EditView中使用,但是这里也可以用。 
android:inputMethod为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。 android:inputType设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。 
android:linksClickable 设置链接是否点击连接,即使设置了autoLink。 
android:ems 设置TextView的宽度为N个字符的宽
度。这里测试为一个汉字字符宽度  
android:maxLength 限制显示的文本长度,超出部分不显示。 
android:lines 设置文本的行数,设置两行就显示两行,即使第二行没有数据。 
android:maxLines 设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 
android:minLines 设置文本的最小行数,与lines类似。 
android:lineSpacingExtra 设置行间距。 
android:lineSpacingMultiplier 设置行间距的倍数。如”1.2” 
android:numeric 如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。 
android:password 以小点”.”显示文本 
android:phoneNumber 设置为电话号码的输入方式。 
android:scrollHorizontally 设置文本超出TextView的宽度的情况下,是否出现横拉条。 
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_width="20dp"将只显示“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。 
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 的区别看这里。 
android:maxWidth 设置文本区域的最大宽度 
android:minWidth 设置文本区域的最小宽度

4、重要方法:

对应属性的getter和setter方法。

6、实战:

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical" ><TextView     android:id="@+id/text1"    android:layout_width="match_parent"    android:layout_height="180dp"    android:gravity="center"    android:text="text1朋朋"    android:textSize="20sp"    android:background="#ff0000"    android:layout_marginLeft="20dp"    android:layout_marginTop="20dp"    android:layout_marginRight="20dp"    android:layout_marginBottom="20dp"    android:    />    </LinearLayout>

参考1:http://developer.android.com/intl/zh-cn/reference/android/widget/TextView.html

参考2:http://m15031966454.blog.163.com/blog/static/201261060201212754843472/

0 0
原创粉丝点击