常用UI组件(widgets)之文本框(TextView)(一)

本文详细介绍了Android中的TextView组件,包括基本用途、属性配置方法以及如何实现文本链接功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)TextView是Android中最基础常见的视图组件,主要用于显示文字提示。

如下main.xml文件:

</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/text"
        android:text="@string/name_text"/>
    

</RelativeLayout>

</pre></p><p><span style="font-family:Microsoft YaHei;font-size:14px;">下图是TextView类的结构关系图:</span></p><p><table border="1" width="200" cellspacing="1" cellpadding="1"><tbody><tr><td>java.lang.Object</td></tr><tr><td><table border="1" width="200" cellspacing="1" cellpadding="1"><tbody><tr><td>        android.view.View      </td></tr></tbody></table>                    android.widget.TextView                 </td></tr></tbody></table><span style="font-family:Microsoft YaHei;font-size:14px;">从关系图中可以知道TextView的直接父类是android.view.View。其实在Android中View类是所有视图组件类的父类,View包含了用户交互和显示,类似于Windows操作系统中的窗口。TextView的直接子类有按钮Button,文本编辑框EditText等,间接子类有自动完成提示的AutoCompleteTextView,复选框CheckBox等。</span></p><p><span style="font-family:Microsoft YaHei;font-size:14px;"><1>android:layout_centerHorizontal="true"表示TextView在父布局(RelativeLayout)中垂直居中显示;</span></p><p><span style="font-family:Microsoft YaHei;font-size:14px;"><2>android:layout_centerVertical="true"<span style="font-family: 'Microsoft YaHei';font-size:14px;">表示TextView在父布局(RelativeLayout)中水平居中显示;</span></span></p><p><span style="font-family:Microsoft YaHei;font-size:14px;">(2)使用XML为TextView设置文字的颜色,字体和文字大小等属性</span></p><p><span style="font-family:Microsoft YaHei;font-size:14px;">修改上面的main.xml布局文件</span></p><p><span style="font-family:Microsoft YaHei;font-size:14px;"></span><pre name="code" class="html"><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/text"
        android:text="@string/name_text"

        android:textColor="#0000c6"
        android:textStyle="bold"
        android:textSize="25px"/>
    
</RelativeLayout>



Android中的颜色值是十六进制的GRB.

<1>android:"@+id/text"表示对TextView的唯一标识,有了id后就可以在Java代码中创建该TextView的实例对象;

<2>android:textColor="#0000c6"表示为TextView中的文字设置颜色,这里设置颜色为蓝色;

<3>android:textStyle="bold"表示设置TextView中的文字的字体,这里设置成粗体;

<4>android:textSize="25px"表示设置TextView中的文字的大小,这里设置为25px;

(3)让文本网址自动链接到互联网

平时上网时单击一个类似于"http://www.xxx.com"的网址后就会跳转到该网站,然后可以看到网站返回的内容,在Android的TextView视图组件中有一个autolink属性用于单击TextView中给定的网址后跳转到该网址对应的网站,然后就可以看到此网站的内容。修改main.xml文件:

<RelativeLayout 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:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        
        android:autoLink="all"
        android:text="www.baidu.com"

        android:textColor="#0000c6"
        android:textStyle="bold"
        android:textSize="25px"/>

</RelativeLayout>
<1>android:text="www.baidu.com"是要访问的网址

<2>android:autoLink="all"表示自动链接到指定的网址,这里的all表示匹配所有的链接

 

TextView的android:autoLink的链接属性值的种类如下表所示:

autoLink的属性值作用
all表示匹配一切的链接方式
None表示不匹配任何链接方式
Phone表示匹配电话号码链接方式
Email表示匹配电子邮件链接方式
Web表示匹配互联网网址链接方式
Map表示匹配映射地址链接方式
当在TextView标签中的android:autoLink设置了表格中的属性值后,如果文本值匹配设置的属性值,那么Android系统会根据这个链接调用Android内部集成的对应处理软件,将这些文本值转成可以单击的链接,用户单击后就可以导航到相应的界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值