編輯:關於 Android 程式設計
想練手感,做登入頁是很好的題目:它同時涵蓋背景圖、圖示、輸入框、狀態切換與按鈕樣式,卻又不會複雜到失控。以下用純 XML 版面把一個近似 QQ 風格的登入頁拆開來看。
最外層用 RelativeLayout,方便讓「Logo 區塊」、「資料輸入區塊」、「登入按鈕」三者之間建立相對位置關係。Logo 用一個置中的水平 LinearLayout 包住 ImageView,並以 layout_above="@+id/login_data_layout" 掛在輸入區上方;登入按鈕則用 layout_below 掛在輸入區下方,中間留 30dp 間距。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back_groud">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/login_data_layout"
android:layout_marginBottom="30dp"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo" />
</LinearLayout>
<LinearLayout
android:id="@+id/login_data_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/logindata_back_groud"
android:orientation="vertical">
...
</LinearLayout>
<Button
android:id="@+id/login_but_landing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_data_layout"
android:layout_marginTop="30dp"
android:background="@drawable/login_but_bg"
android:text="登 錄"
android:textColor="#ffffff"
android:textSize="18dp" />
</RelativeLayout>
輸入區本身是個直向的 LinearLayout,內含兩個水平列。每一列都由「提示文字 + EditText +(可選的)小按鈕」組成,並用 layout_weight 讓 EditText 吃掉剩餘寬度。
幾個值得注意的屬性:
android:background="@null":拿掉 EditText 預設的下劃線,讓它融入整塊背景圖;android:singleLine="true":帳號欄保持單行,避免換行破版;android:inputType="textPassword":密碼欄的遮罩與輸入法模式都由這一行決定;layout_alignParentRight 貼齊右側,外面再包一層 RelativeLayout 並給 layout_weight="2.5",讓它被推到列表最右端。
<EditText
android:id="@+id/login_edit_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="@null"
android:singleLine="true" />
點下拉箭頭後彈出的浮動層,本質上就是一個 ListView,外層用直向 LinearLayout 置中:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_backgroud" />
</LinearLayout>
清單的每一列則是「文字 + 刪除鈕」的組合,刪除鈕用 25dp 貼右,文字則以 layout_toLeftOf="@+id/del_but" 自動避開按鈕,這是不寫死寬度的關鍵寫法:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical">
<Button
android:id="@+id/del_but"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:background="@drawable/del_but_bg" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="@+id/del_but"
android:textSize="16dp" />
</RelativeLayout>
整份版面完全沒有寫死螢幕像素,全部靠 match_parent、weight 與相對定位撐開,因此在不同解析度的機子上都能維持一致的比例。真正的登入邏輯(比對帳密、切換 Activity)只要掛在 login_but_landing 的 click listener 上就完成了。
Android BLE 開發:封裝一個通用藍牙工具類,讀取小米手環步數
物聯網正熱,小米手環這類可穿戴設備幾乎人手一只。本文示範如何封裝一套通用的 BLE(Bluetooth Low Energy)工具類,並用它讀取小米手環的運動數據。 一、
Android 自製相簿瀏覽效果:Gallery 與自訂 ImageAdapter
要做一個可橫向滑動瀏覽縮圖、點選後在主區顯示大圖的相簿效果,用 Gallery 搭配自訂的 BaseAdapter 是最經典的組合。 執行效果 C# 實作 us
Android ListView 載入動畫的實作方式
ListView 是 Android 開發中最常使用的控制項之一,但千篇一律的外觀難免讓人覺得單調。若能為列表加入載入動畫,不僅能提升視覺層次,也能帶來更流暢的使用者體驗
Android SlidingMenu 側滑菜單使用詳解:從入門到實戰
許多 App 都具備側滑菜單功能,部分甚至左右兩側皆有。開源專案 SlidingMenu 能很好地幫助我們實現側滑效果。本文先透過範例介紹幾種常見的引入方式,接著展示一個