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

逃离我推掉我的手 2022-08-06 14:29 245阅读 0赞

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

如下main.xml文件:

  1. </pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:layout_centerHorizontal="true"
  10. android:layout_centerVertical="true"
  11. android:id="@+id/text"
  12. android:text="@string/name_text"/>
  13. </RelativeLayout>
  14. </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"?>
  15. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. tools:context=".MainActivity">
  19. <TextView
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_centerHorizontal="true"
  23. android:layout_centerVertical="true"
  24. android:id="@+id/text"
  25. android:text="@string/name_text"
  26. android:textColor="#0000c6"
  27. android:textStyle="bold"
  28. android:textSize="25px"/>
  29. </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文件:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <TextView
  7. android:id="@+id/text"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_centerHorizontal="true"
  11. android:layout_centerVertical="true"
  12. android:autoLink="all"
  13. android:text="www.baidu.com"
  14. android:textColor="#0000c6"
  15. android:textStyle="bold"
  16. android:textSize="25px"/>
  17. </RelativeLayout>

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

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

Center Center 1

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
































autoLink的属性值 作用
all 表示匹配一切的链接方式
None 表示不匹配任何链接方式
Phone 表示匹配电话号码链接方式
Email 表示匹配电子邮件链接方式
Web 表示匹配互联网网址链接方式
Map 表示匹配映射地址链接方式

当在TextView标签中的android:autoLink设置了表格中的属性值后,如果文本值匹配设置的属性值,那么Android系统会根据这个链接调用Android内部集成的对应处理软件,将这些文本值转成可以单击的链接,用户单击后就可以导航到相应的界面。

发表评论

表情:
评论列表 (有 0 条评论,245人围观)

还没有评论,来说两句吧...

相关阅读

    相关 微信小程序UI

    开发微信小程序的过程中,选择一款好用的组件库,可以达到事半功倍的效果。自从微信小程序面世以来,不断有一些开源组件库出来,下面5款就是排名比较靠前,用户使用量与关注度比较高的小程