TextView的父类是View类,View类为所有可视化控件的基类,主要提供了控件的绘制和事件处理的方法。
创建用户界面所使用的空间都继承自“View”类,比如:TextView控件、Button控件、CheckBox控件等。
关于View及其子类的相关属性,可以在布局XML文件中进行设置。也可以通过成员函数在代码中动态设置。
任何继承自View的子类都将拥有View类的属性及对应的方法,由于TextView类也是继承自View类,所以View的属性在TextView类也包含。
TextView控件的主要功能是向用户显示文本内容,同时可选择性地让用户编辑文本。
例如,举例一个应用,利用TextView控件实现显示一个网址,可以单击这个网址,应用会自动转到浏览器打开这个网页。
其中res/layout中的activity_main.xml文件的内容改为如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFBBAA"
android:textSize="20px"
android:layout_marginBottom="10px"
android:singleLine="true"
android:autoLink="all"
android:text="@string/html" />
</LinearLayout>
res/values下的string.xml文件内容改为:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myTextView</string>
<string name="action_settings">Settings</string>
<string name="html">www.baidu.com</string>
</resources>
Java源代码内容不用改动,就可以实现了在界面上显示文本内容了