android开发日记3----UI编程(TextView)

文本框(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);
    }
}

运行效果

 

 

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



 

 

 

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

代码片段:
<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="20pt"
        android:textColor="#ff0000"
        />
 

猜你喜欢

转载自liudewen.iteye.com/blog/1965729
今日推荐