Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android GUI 基本組件介紹:TextView、Button 與更多

編輯:關於 Android 開發

本文將介紹幾個在 Android GUI 開發中最常用的基本組件,並附上各自的 XML 布局寫法,方便快速上手。

1. TextView:顯示文字的標籤

TextView 是最基礎的文字顯示組件,其 XML 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, my name is TextView, I am mainly show some text."
    />

其他常用屬性還包括:android:typeface(字體)、android:textStyle(字形樣式)、android:textColor(文字顏色)等。

2. Button:按鈕控件

按鈕是 TextView 的子類,其 XML 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="https://schemas.android.com/apk/res/android"
    android:text="Hello, my name Button, I am the subclass of TextView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

3. ImageView 與 ImageButton:圖像控件

它們分別是 TextView 與 Button 的圖像類似物,都透過 android:src 屬性指定圖片位置。ImageView 的示例如下:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    android:src="@drawable/ffffgggg"
    />

4. EditText:可輸入文字的文字框

EditText 同樣是 TextView 的子類,另外還有一些專屬屬性,如 android:autoText、android:capitalize、android:digits、android:singleLine 等。其簡單的 XML 布局如下:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/field"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="You can type words here..."
    android:singleLine="false"
    />

掌握這些基本組件,就已經能組建出大多數簡單的 Android 界面了。隨著實作經驗累積,再搭配各種布局容器,就能靈活打造豐富的用戶介面。

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