編輯:關於 Android 程式設計
LinearLayout 是最基礎也最常用的 ViewGroup,它把子元素排成一條直線——水平或垂直。本文以一個「四顆按鈕等寬並排」的簡單畫面為例,把佈局檔、字串資源、樣式、R.java 與 Activity 五個環節逐一走過,適合當作 Android 資源體系的第一個練習。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MyActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/wo"
android:textColorHint="@color/calamus" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/shi" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/hao"
android:textColor="@color/calamus" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/ren" />
</LinearLayout>
幾個值得注意的地方:android:orientation="horizontal" 決定排列方向(預設是 horizontal);四顆按鈕都帶 layout_weight="1",因此會等寬瓜分整個寬度;tools:context 只在 Android Studio 的視覺編輯器中生效,不會被打進 APK,用來讓預覽正確套用主題。
另外示範了 textColorHint 與 textColor 的差別:前者是「提示文字」的顏色,對 Button 而言通常看不到效果;後者才是真正的文字顏色。範例中刻意並列兩者,正好說明這個常見誤用。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Linear</string>
<string name="action_settings">Settings</string>
<string name="wo">我</string>
<string name="shi">是</string>
<string name="hao">好</string>
<string name="ren">人</string>
</resources>
把畫面上的文字抽到資源檔而不是寫死在佈局裡,是 Android 開發的基本紀律。這麼做才能在同一份佈局下,靠 values-en、values-ja 等不同資料夾提供多語系版本,而不必複製多份佈局檔。
<resources>
<!-- 基礎主題,依 API level 而異。新版裝置會由 res/values-vXX/styles.xml
中的 AppBaseTheme 取代這裡的定義。 -->
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- 應用主題,非特定 API level 的客製項目放在這裡。 -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
<color name="calamus">#c77eb5</color>
</resources>
這種「AppBaseTheme + AppTheme」的兩層結構是舊版 Android Studio 產生的樣板。設計意圖是:針對特定 API level 的差異性樣式放在 values-v11、values-v14 等資料夾中各自覆寫 AppBaseTheme,而跨版本共通的客製則集中在 AppTheme。顏色同樣建議抽成資源,日後換色只要改一處。
這個檔案由 aapt 工具在編譯時自動產生,內容會隨資源變動而重新生成,任何情況下都不要手動修改。
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found.
* It should not be modified by hand.
*/
package com.eirc.linear;
public final class R {
public static final class attr { }
public static final class color {
public static final int calamus=0x7f070000;
}
public static final class dimen {
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int LinearLayout=0x7f090000;
public static final int action_settings=0x7f090005;
public static final int button1=0x7f090001;
public static final int button2=0x7f090002;
public static final int button3=0x7f090003;
public static final int button4=0x7f090004;
}
public static final class layout {
public static final int activity_my=0x7f030000;
}
public static final class menu {
public static final int my=0x7f080000;
}
public static final class string {
public static final int action_settings=0x7f050001;
public static final int app_name=0x7f050000;
public static final int hao=0x7f050004;
public static final int ren=0x7f050005;
public static final int shi=0x7f050003;
public static final int wo=0x7f050002;
}
public static final class style {
public static final int AppBaseTheme=0x7f060000;
public static final int AppTheme=0x7f060001;
}
}
從這個檔案可以讀懂 Android 資源的組織方式:每一類資源(color、dimen、drawable、id、layout、menu、string、style)各有一個內部類別,每個資源對應一個 int 常數。佈局檔中寫的 @string/wo、@color/calamus、@+id/button1,編譯後都會變成這裡的某個整數。而 R.layout.activity_my 之所以能在 Activity 中使用,正是因為它在這裡被定義了。
package com.eirc.linear;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 讓選單項目出現在 Action Bar 上
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
}
setContentView(R.layout.activity_my) 把 XML 定義的畫面裝上 Activity;onCreateOptionsMenu() 則透過 MenuInflater 把 res/menu 下的選單定義展開到 Action Bar。回傳 true 表示選單要顯示,回傳 false 則會隱藏。
畫面上會看到「我是好人」四顆等寬按鈕並排,其中第三顆的文字呈現 #c77eb5 這個偏紫的粉紅色,正是 styles.xml 中 calamus 顏色的效果。
把 android:orientation 改成 vertical,觀察按鈕如何變成縱向排列;再把其中一顆按鈕的 layout_weight 改成 2,看看寬度如何重新分配。這兩個小實驗能幫你快速建立對 LinearLayout 的直覺。若需要更複雜的巢狀比例或表格排列,則可進一步研究 RelativeLayout、ConstraintLayout 與 TableLayout 的適用時機——ConstraintLayout 在現代專案中已是首選,它能以扁平層級完成絕大多數 LinearLayout 加 RelativeLayout 才能做到的效果,對效能與可維護性都更有利。
用 WIFI 讓 Android 手機與 Android Studio 連線,實機測試 App
想擺脫 USB 線、用手機無線連線做實機測試嗎?以下步驟示範如何透過 WIFI 完成設定。 1、讓手機取得 ROOT 權限(例如小米 note,直接下載安裝 MIUI 開
Android MVP 十分鐘入門:用一個下載功能看懂 MVC 與 MVP 的差別
為什麼需要 MVP 專案規模變大之後,程式碼量也跟著膨脹,每個類別裡的內容越塞越多。Activity 與 Fragment 尤其嚴重——由於 Context 的存在,幾
Android TV 磁貼類 App 自動化框架改造筆記(以 UiAutomator 為基礎)
為什麼要自己動手 專案長期以手工測試為主,加上產品是 TV 類應用,市面上多數自動化工具並沒有針對 TV 場景做良好適配,最後只能自己改造一套。以下做法源自這個專案的特殊
深入解析 android.app.Activity 與其生命週期
目前網路上關於 Android 的資料不算多,針對 Activity 的說明更少,因此我把官方文件中 android.app.Activity 的內容翻譯整理,並加入一些