編輯:關於 Android 程式設計
本節我們來看看登入頁面該如何配置。對於剛接觸 Android 開發的朋友而言,Android 的版面配置往往令人感到棘手,需要結合各種屬性進行設定。接下來我們由淺入深,了解安卓中頁面配置的方式。登入頁面很簡單,只有兩個文字框與一個按鈕,效果如下:

點擊 activity_main.xml 可查看版面配置程式碼,並可切換到 graphical Layout 檢視設計窗體。全部程式碼如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="0.9"
android:layout_height="fill_parent">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_weight="0.1"
android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:textSize="8pt"
android:text="用戶名"
android:layout_weight="0.75"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText
android:layout_weight="0.25"
android:layout_width="fill_parent"
android:text="請輸入用戶名"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:textSize="8pt"
android:text="密碼"
android:layout_weight="0.75"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText
android:layout_weight="0.25"
android:layout_width="fill_parent"
android:password="true"
android:text="請輸入密碼"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:text="登錄"
android:textSize="9pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:gravity="center"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:text="注冊 "
android:textSize="9pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
控制項介紹
LinearLayout 為線性版面配置控制項,顧名思義就是以線性方式展示,包括水平與垂直兩種。 android:orientation="vertical" 表示垂直方向,android:orientation="horizontal" 表示水平方向。
TextView 為顯示控制項,類似於 label 標籤;EditText 為文字方塊控制項,類似於 TextBox;Button 則為按鈕控制項。
屬性說明
layout_width:表示控制項相對於父容器的寬度,有三個值:fill_parent、wrap_content、match_parent。fill_parent 表示填滿整個螢幕寬度,wrap_content 表示匹配內容寬度。
android:orientation:設定水平或垂直方向。
android:layout_weight:設定控制項所占的百分比。
android:gravity="center":設定控制項內元素的位置。
android:layout_gravity:用來控制控制項在父容器中的位置。
Android 原始碼剖析:Handler 與 Looper 訊息機制完整解說
Android 的應用程式與傳統 Java 程式一樣,靠訊息驅動運作。說得更具體些:系統維護一個訊息佇列,我們可以持續把訊息放進去,也可以從中取出來處理。擔起這份工作的三
android:descendantFocusability 用法解析:解決 ListView item 點擊沒反應
這是一個開發中很常遇到的怪現象:自訂 ListView 的 item 裡放了 Button、ImageButton 或 CheckBox 這類子控件,結果點擊整列完全沒反
Android 自訂義圓形進度條:打造清晰的任務完成視覺
在 Android 介面設計中,圓形進度條常用於呈現任務完成比例、課程進度或資料載入狀態。相比傳統橫向進度條,圓形設計更容易聚焦視線,也更能營造現代感。實作這類效果時,可
Android Framework 中的 Camera 類關係全景圖:ICameraClient、ICamera 與 CameraClient
梳理 Android Framework 的 Camera 相關類時,最容易暈的地方在於同名與近名類層出不窮:Java 層的 Camera、Native 層的 Camer