Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android UI 控件系列:TextView 文本框

編輯:Android 資訊

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——功能與 layout_width/height 相似

區別:android:layout_width 隻能設 fill_parent(橫向填滿屏幕)或 wrap_content(填滿控件自身大小);而 android:width 用於設置具體像素寬度。

例如:TextView 顯示。

main.xml 布局文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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); } }

運行結果:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved