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

Android 基礎 GUI 組件速覽

編輯:Android 開發實例

Android 的界面控件大多可以在 XML 布局中直接聲明,屬性配置直觀,改動後無需重編代碼即可預覽效果。下面梳理幾個最常用的基礎組件。

1. TextView:文本標籤

最基礎的文本展示控件,其他多個組件都由它派生而來。

<?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 maily show some text."
/>

常用附加屬性還有 android:typeface(字體)、android:textStyle(粗體/斜體)、android:textColor(文字顏色)、android:textSize(字號)等。

2. Button:按鈕

Button 是 TextView 的子類,因此 TextView 的所有顯示屬性它都能用,只是外觀被換成了可點擊的樣式。

<?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"/>

點擊事件則需要在代碼中透過 setOnClickListener() 綁定。

3. ImageView 與 ImageButton:圖像展示

這兩者分別是 TextView 與 Button 的圖像版本,核心屬性是 android:src,用於指定圖片資源;android:adjustViewBounds 則控制是否按圖片實際比例調整邊界,避免圖形被拉伸變形。

<?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:文本輸入

同樣是 TextView 的子類,專門用於接收用戶輸入。除了繼承來的屬性,它還有一批控制輸入行為的關鍵屬性:android:autoText(自動補全)、android:capitalize(首字母大寫)、android:digits(限定可輸入字符)、android:singleLine(是否單行)、android:inputType(鍵盤類型)等。

<?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:hint 代替 android:text 來給出提示文字,因為 hint 在用戶開始輸入後會自動消失,而 text 會一直留在輸入框裡需要手動清空。

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