Android控件_TextView(显示文本框控件)

时间:2023-03-08 21:01:35

一、TextView控件的常用属性

     1、android:id——控件的id

      2、android:layout_width——设置控件的宽度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)

    3、android:maxWidth——设置控件的最大宽度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)
    4、android:minWidth——设置控件的最小宽度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)

     5、android:layout_height——设置控件的高度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)

    6、android:maxHeight——设置控件的最大高度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)
    7、android:minHeight——设置控件的最小高度

        wrap_content(包裹实际文本内容)

        fill_parent(当前控件铺满父类容器)

        match_parent(当前控件铺满父类容器,2.3api之后添加一个属性值)

        支持度量单位:px(像素)/dp/sp/in/mm(毫米)
    8、android:background——设置控件的背景颜色

        可以使用十六进制(可以直接在左边选择颜色),也可以在资源文件里设置好颜色值后通过id的形式引用

    9、android:textColor——设置文字颜色

        可以使用十六进制(可以直接在左边选择颜色),也可以在资源文件里设置好颜色值后通过id的形式引用

      10、android:text——文本内容

        设置文本内容的时候可以直接写文字,但最好在string.xml中设置好文字后通过id引用

    11、android:textSize——设置文字大小

        推荐使用单位"sp"

    12、android:textStyle——字体样式

        [bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开

    13、android:typeface——设置文本字体

        必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]

    14、android:textScaleX——设置文字之间间隔

        默认为1.0f

    15、android:lines——设置文本的行数

        设置两行就显示两行,即使第二行没有数据。

     16、android:singleLine——是否单行显示

        如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。

        android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行

    17、android:maxLines——设置文本的最大显示行数

        与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。

    18、android:minLines——设置文本的最小行数

        与lines类似。

     19、android:autoLink:设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。

         可选值(none/web/email/phone/map/all)

     20、android:textColorLink——文字链接的颜色. 

         可以使用十六进制(可以直接在左边选择颜色),也可以在资源文件里设置好颜色值后通过id的形式引用

     21、android:gravity——设置文字对齐方式

         如设置成“center”,文本将居中显示。

     22、android:textIsSelectable——允许用户做出选择的手势,进而触发系统内置的复制/粘贴控制

        值可以为"true/false"

    23、android:drawableRight在text的右边输出一个drawable。 
    24、android:drawableTop在text的正上方输出一个drawable。 

    25、android:drawableBottom在text的下方输出一个drawable

    26、android:drawableLeft在text的左边输出一个drawable

    27、android:drawablePadding设置text与drawable(图片)的间隔

        与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。

       

二、代码示例

<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="wrap_content"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="@+id/textView"
android:textIsSelectable="true"
android:background="#ff37af"
android:textSize="25sp"
android:textColor="#0942ff"
android:textScaleX="2.0f"
android:gravity="right"
android:lines="2"
android:autoLink="email"
android:textColorLink="#03ff22"
android:textColorHighlight="#02e9ff"
android:textStyle="italic|bold"/>
</LinearLayout>