简介
在Android的UI中,文本框用TextView组件表示,用于在界面上显示文字信息。TextView组件不仅能显示单行文本,也能显示多行文本,甚至可以显示带图像的文本。
TextView常用属性
android:autoLink | 设置当文本为URL链接,email,电话号码时是否显示为可单击的链接。可选值:none,web,email,phone,map,all |
android:drawableButtom | 在文字下方放置图片。还有类似的左方,上方,右方 |
android:gravity | 设置文本位置,如center居中 |
android:hint | Text为空时的提示信息 |
android:maxLength | 文本最大长度,超过部分不显示 |
android:lines | 设置文本的行数 |
android:singleLine | 设置单行显示,超过的用“.....”表示 |
android:textColor | 设置文本颜色 |
android:textSize | 设置文本字体大小 |
android:textStyle | 设置字形。可选值:bold(粗体)0,italic(斜体)1,bolditalic(粗斜)2,多个值用"|"隔开 |
android:typeface | 设置文本字体:normal0,sans1,serif2,monospace3 |
android:height | 文本域高度 |
android:width | 文本域宽度 |
实战
- 创建一个空activity的项目
- 在xml布局文件修改布局样式,改为LinearLayout线性布局,然后添加textView控件
- 在activity类中根据ID获取控件
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- <TextView-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:id="@+id/text1"-->
<!-- android:text="欢迎进入后台管理系统!"-->
<!-- android:height="100dp"-->
<!-- android:textSize="50dp"-->
<!-- android:textColor="#F00"-->
<!-- android:gravity="center"-->
<!-- ></TextView>-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/text2"
android:text="www.baidu.com"
android:autoLink="all"
android:height="100dp"
android:textSize="50dp"
android:gravity="center"
></TextView>
</LinearLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="欢迎进入后台管理系统!"
android:height="100dp"
android:textSize="50dp"
android:textColor="#F00"
android:gravity="center"
></TextView>
<!-- <TextView-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:id="@id/text2"-->
<!-- android:text="www.baidu.com"-->
<!-- android:autoLink="all"-->
<!-- android:height="100dp"-->
<!-- android:textSize="50dp"-->
<!-- android:gravity="center"-->
<!-- ></TextView>-->
</LinearLayout>