Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學習筆記:仿 QQ 登入界面的版面配置實作

編輯:關於 Android 程式設計

想練手感,做登入頁是很好的題目:它同時涵蓋背景圖、圖示、輸入框、狀態切換與按鈕樣式,卻又不會複雜到失控。以下用純 XML 版面把一個近似 QQ 風格的登入頁拆開來看。

主界面:RelativeLayout 打底

最外層用 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":密碼欄的遮罩與輸入法模式都由這一行決定;
  • 下拉箭頭按鈕用 24dp 見方,以 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 上就完成了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved