---
title: "android开发日记3----UI编程(TextView)"
author: "hbldw521"
date: "2013-10-28T16:48:42Z"
tags: []
categories: ["移动开发"]
slug: "android-3-ui-textview"
draft: false
lastmod: "2013-10-28T17:19:47Z"
id: 1965729
url: "https://liudewen.iteye.com/blog/1965729"
---

**文本框（TextView）**  
TextView直接继承了View,它还是EditText、Button两个UI组件类的父类。TextView的作用就是在界面上显示文本---从这个意义上来看，它有点类似于Swing编程中的JLabel,不过它比JLabel功能更强大

**代码示例**

package com.apk;

import android.app.Activity;  
import android.os.Bundle;  
import android.widget.TextView;

public class TextViewUIActivity extends Activity {  
    /\** Called when the activity is first created. \*/  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        TextView tv = new TextView(this);  
        tv.setText("测试UI组件--TextView");  
        setContentView(tv);  
    }  
}

**运行效果**

   
![](http://dl2.iteye.com/upload/attachment/0090/6280/b3935991-04b6-3775-a188-e34b403edbd2.png)  
 

TextView和EditText具有很多相似之处，它们之间的最大区别在于TextView不允许用户编辑文本内容，而EditText则允许用户编辑文本内容。  
TextView提供了大量的XML属性，这些XML属性大部分即可适用于TextView，又可以适用于EditText,但有少量XML只能适用于其中之一。

 

![](http://dl2.iteye.com/upload/attachment/0090/6296/0e9326d3-2761-3a4c-93e1-fd060b71fd6b.png)  
   
![](http://dl2.iteye.com/upload/attachment/0090/6298/0e9dc539-1cf6-34ef-b172-559f2f62ad6b.png)  
   
![](http://dl2.iteye.com/upload/attachment/0090/6300/292c0e93-9908-3970-a438-09c97628a213.png)  
 

我们可以通过如上表中列出的XML属性在界面布局文件中控制TextView中文本的行为(如：颜色，字体等)

代码片段:  
&lt;TextView  
        android:layout\_width="fill\_parent"  
        android:layout\_height="wrap\_content"  
        android:text="@string/hello"  
        android:textSize="20pt"  
        android:textColor="#ff0000"  
        /&gt;