Android基础UI组件(一):TextView文本框

mac2024-06-16  48

简介

在Android的UI中,文本框用TextView组件表示,用于在界面上显示文字信息。TextView组件不仅能显示单行文本,也能显示多行文本,甚至可以显示带图像的文本。

TextView常用属性

 

android:autoLink设置当文本为URL链接,email,电话号码时是否显示为可单击的链接。可选值:none,web,email,phone,map,allandroid:drawableButtom在文字下方放置图片。还有类似的左方,上方,右方android:gravity设置文本位置,如center居中android:hintText为空时的提示信息android:maxLength文本最大长度,超过部分不显示android:lines

设置文本的行数

android:singleLine设置单行显示,超过的用“.....”表示android:textColor设置文本颜色android:textSize设置文本字体大小android:textStyle设置字形。可选值:bold(粗体)0,italic(斜体)1,bolditalic(粗斜)2,多个值用"|"隔开android:typeface设置文本字体:normal0,sans1,serif2,monospace3android: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>

最新回复(0)