編輯:關於 Android 程式設計
java.lang.Object
→ android.view.View
→ android.widget.TextView
→ android.widget.Button
這個繼承鏈說明了兩件事:Button 具備 View 的全部屬性與事件能力,同時因為繼承自 TextView,所以文字相關的設定(顏色、大小、內容)都能直接沿用。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/mybut1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="12px"
android:text="按鈕組件" />
</RelativeLayout>
layout_width / layout_height 設為 wrap_content:寬高都依文字內容自動伸縮。android:id="@+id/mybut1":定義元件的 id,供 Activity 以 findViewById 取得。android:textColor="#FF0000":以 RGB 十六進位設定文字顏色,這裡是紅色。android:textSize="12px":設定文字大小,px 是像素單位。android:text="按鈕組件":設定按鈕上顯示的文字。另外要留意父容器的設定:範例中 RelativeLayout 的寬高都是 fill_parent,也就是佔滿整個螢幕;若改成 LinearLayout 並設 android:orientation="vertical",多個元件就會垂直排列。

實際寫專案時,textSize 建議改用 sp 單位(例如 16sp),這樣才能跟著使用者的系統字級縮放偏好一起變,避免在視力需求不同的裝置上變得難以閱讀。
Android Studio 必備常用插件整理:ButterKnife、Codota 與 Genymotion
Android Studio 的插件生態相當豐富,以下整理常見且能提升開發效率的工具。 ButterKnife 由 Jake Wharton 開發,可用來快速綁定 Vi
Android 開發常見陷阱與優化建議整理
Android 開發中,很多問題並不是因為功能太難,而是因為忽略了細節。記憶體洩漏、UI 崩潰、效能下降、相容性問題,往往都藏在看似普通的寫法裡。以下整理常見開發陷阱與實
Android WebView 原理剖析與學習計劃
我們常在 App 的 UI 中嵌入 WebView,用以實現某些功能的動態更新。在 Android 4.4 之前,WebView 基於 WebKit 實現;4.4 之後則
Android 專案導入第三方 jar 的正確方式
常見錯誤在 Android 專案加入第三方 jar 後,若執行時出現找不到類別的錯誤,通常不是程式碼本身問題,而是 jar 沒有被正確加入建置路徑。ADT 版本差異在 A