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

Android 登入介面佈局實作:比例排版與隱藏標題列、全螢幕覆蓋

編輯:關於 Android 程式設計

在打造 Android 登入介面時,有幾個實務上經常踩到的細節值得先記住:圖片資源盡量放在 drawable 一級目錄下,且檔案命名只能使用小寫字母、數字與底線;此外,若希望介面呈現乾淨的沉浸式效果,往往需要隱藏應用標題列並讓內容全螢幕覆蓋。

以下先來看佈局的核心寫法。這裡大量運用 layout_weight 搭配 layout_height="0dp" 的技巧,讓各區塊依比例分配垂直空間,這正是「紅線處按比例設置」的關鍵:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/home_page_bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:gravity="center" ></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:gravity="center_horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/home_page_login" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/home_page_register"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:background="@drawable/button01_a" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:background="@drawable/button02_a" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:background="@drawable/button03_a" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:background="@drawable/button04_a" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:background="@drawable/button05_a" />
    </LinearLayout>
</LinearLayout>

排版要點

上方幾個區塊皆以 layout_weight="1" 平均瓜分剩餘空間,讓登入與註冊按鈕能置中呈現;最底下一列的五個按鈕則各自帶 layout_weight="1",配合寬度設為 0dp,實現等寬平分的效果。至於「介面不顯示應用名稱」與「全螢幕覆蓋」的設定,可透過在 Style 中關閉標題列、或於 Activity 的 onCreate 中呼叫 requestWindowFeature(Window.FEATURE_NO_TITLE) 來達成。

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