Android UI控制元件系列:TextView(文字框)

apkbus發表於2014-12-05

TextView比較簡單,不能夠用來進行編輯,只能夠用來顯示資訊

佈局檔案裡的一些常用的XML屬性

android:gravity—用來設定控制元件內文字的對齊方式
android:layout_gravity—相對於父控制元件來說,用於設定控制元件的對齊方式
android:text—用來設定控制元件文字資訊
android:layout_width—用來設定控制元件的寬度
android:layout_height—用來設定控制元件的高度
android:background—用來設定控制元件的背景色
android:textColor—用來設定控制元件內文字的顏色
android:textSize—用來設定控制元件的文字字型大小
android:width和android:height—功能與android:layout_width相似
區別:
android:layout_width只能設定fill_parent(橫向填充整個螢幕)或

wrap_content(橫向填充控制元件本身大小)
android:width設定具體控制元件的橫向大小 單位是畫素

例如:TextView顯示

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:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

string.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyTestView!</string>
    <string name="app_name">MyTestView</string>
</resources>

MyTextView.java檔案

package org.loulijun.MyTestView;

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

public class MyTestView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

執行結果:

相關文章