TextView的功能与用法
- TextView直接继承了View,它还是EditView,Button两个UI组件的父类。
- TextView与EditView有很多相似之处,他们之间的最大区别是EditView允许用户编辑编辑文本内容,而TextView不允许。
- TextView提供了大量的XML属性,这些属性不仅适用于TextView,TextView的子类也同时适用。
TextView的XML属性
下面列举了TextView一些常用的XML属性及相关方法的说明:
XML属性 | 相关方法 | 说明 |
---|---|---|
android:autoLink | setAutoLinkMask(int) | 是否将符合指定格式的文本转化成可点击的链接的形式。 该属性支持如下属性值: none :没有匹配模式(默认值) web : 匹配URL网址 email : 匹配邮箱地址 phone : 匹配电话号码 map : 匹配映射地址 all : 匹配所有的模式 |
android:drawableTop | setCompoundDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的顶端绘制指定图像 |
android:drawableEnd | setCompoundDrawablesRelativeWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的结尾绘制指定图像 |
android:drawableLeft | setCompoundDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的左边绘制指定图像 |
android:drawableRight | setCompoundDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的右边绘制指定图像 |
android:drawablePadding | setCompoundDrawablePadding(int) | 设置文本框内文本与图像之间的间距 |
android:ellipsize | setEllipsize(TextUtils.TruncateAt) | 设置当显示的文本超过TextView的长度时如何处理文本内容。 该属性支持如下属性值: none :不做任何处理 start : 在文本开始时截断,并显示省略号 middle : 在文本中间截断,并显示省略号 end : 在文本结束时截断,并显示省略号 marquee : 使用marquee滚动动画显示文本 |
android:fontFamily | setTypeface(Typeface) | 设置文本框内文本的字体 |
android:gravity | setGravity(int) | 设置文本框内文本的对齐方式 |
android:hint | setHint(int) | 设置当文本框为空时,把文本框默认显示的文本内容 |
android:lines | setLines(int) | 设置该文本框默认占几行 |
android:shadowColor | setShadowLayer(float,float,float,int) | 设置文本框阴影的颜色 |
android:shadowDx | setShadowLayer(float,float,float,int) | 设置文本框阴影在水平方向的偏移 |
android:shadowDy | setShadowLayer(float,float,float,int) | 设置文本框阴影在垂直方向的偏移 |
android:shadowRadius | setShadowLayer(float,float,float,int) | 设置文本框阴影的模糊程度。该值越大,阴影越模糊。 |
android:text | setText(CharSequence) | 设置文本框的文本内容 |
android:textColor | setTextColor(ColorStateList) | 设置文本框文本的颜色 |
android:inputType | setRawInputType(int) | 指定该文本框的类型。该属性有点像HTML中的input元素type属性。 |
CheckedTextView的功能与用法
TextView派生出了一个CheckedTextView,CheckTextView增加了一个checked状态。
通过setChecked(boolean)和isChecked()方法来改变、访问该组件的checked状态。
通过setCheckMarkDrawable()方法来设置它的勾选图标。
TextView和 CheckedTextView的基本实例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.textviewinfo.MainActivity">
<!--设置字体大小为20sp,文本框右边绘制图片,文本内间距为10dp-->
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:drawableRight="@drawable/picture"
android:text="文本结尾绘制一张图片"/>
<!--设置文本行数为1,文本结尾省略显示-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:text="长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本"/>
<!--对邮件、电话添加链接-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="email|phone"
android:text="邮件是[email protected],电话是16839323939"/>
<!--设置文本颜色,大小,上边距为10dp,并使用阴影-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:shadowColor="#3F51B5"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:text="带有阴影的文字"/>
<!--测试密码框-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"