編輯:Android 開發教學
Android 的介面由各種 View 元件堆疊而成。本節整理開發中最常打交道的一批基礎元件:
TextView——文字顯示元件
Button——按鈕元件
ImageButton——圖片按鈕元件
ImageView——圖片顯示元件
CheckBox——核取方塊元件
RadioButton——單選按鈕元件
AnalogClock——模擬時鐘(帶指針與錶盤)元件
DigitalClock——數位時鐘元件
先用最簡單的 TextView 開個頭。版面配置檔 textview.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 - 文字顯示元件 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
對應的程式碼 _TextView.java:
package com.webabcd.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class _TextView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textview);
// 設定 Activity 的標題
setTitle("TextView");
TextView txt = (TextView) this.findViewById(R.id.textView);
// 設定文字內容,需要換行時使用 "\n"
txt.setText("我是 TextView\n顯示文字用的");
}
}
這批元件都遵循同一套使用模式:先在 XML 版面檔中宣告外觀與初始屬性,再在 Activity 的 onCreate() 裡透過 findViewById() 取得參考,接著動態設定文字、圖片或監聽器。掌握這個流程,其餘元件的用法基本可以類推。
Android 開發入門:AnalogClock 與 DigitalClock
AnalogClock 可顯示類比時鐘,包含時針與分針;DigitalClock 則以數字方式顯示時間。兩者都會顯示系統時間,若需要特定時區,通常需自行實作自訂控制項。使
Android 與 iPhone 實用體驗對比:一個安卓老用戶的換機心得
注 1:以下功能均以 MIUI ROM 為例說明,若你的 Android 沒有類似功能,可能是版本差異,部分功能需安裝 App 才能實現。 注 2:以下優缺點純屬個人觀感
Android 簡明開發教程八:引路蜂二維繪圖範例的功能定義
有了前面對 Android 平台的介紹,基本上就可以開始動手寫應用了。這裡以繪製二維圖形為例,把 Android 開發的一般方法整個走一遍。 這個範例會練到哪些技能 看似
iOS 6、iOS 7、Android 與 Windows Phone 8 的使用者體驗對比
技術顧問公司 Pfeiffer 進行了一次行動作業系統的使用者體驗研究,目的是收集並比較可量化的指標,對象包括 Apple iOS 6、全新設計的 iOS 7、微軟 Wi