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

Android 仿 UC 底部菜單欄的實作原理與程式碼

編輯:關於 Android 程式設計

看完 ViewPager 之後,就想做一個仿 UC 風格的底部菜單 Demo。一開始打算用自訂的 imgBtn,但發現放在 pager 的選項卡裡不好排版,最後改採 GridView——簡單、實用,四宮格自動等分。

一、主介面配置 activity_main.xml

結構分三層:頂部一個切換顯示/隱藏的按鈕,中間放幾個自訂的 MyImgBtn,底部則是可展開的選單區(含標籤列、動畫游標層與 ViewPager)。

<RelativeLayout 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:background="@drawable/bg"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="show/hide Menu" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_menu"
        android:gravity="center"
        android:orientation="horizontal" >

        <com.example.myMenu.MyImgBtn
            android:id="@+id/main_btn1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_weight="1" />
        <!-- main_btn2 / main_btn3 / main_btn4 結構相同 -->
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout_menu"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_alignParentBottom="true" >

        <!-- 標籤列:常用 / 工具 / 設置 -->
        <LinearLayout
            android:id="@+id/menu"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#dd000000"
            android:gravity="center" >
            <TextView android:id="@+id/tv_main"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:gravity="center"
                android:text="常用" android:textColor="#ffffffff" />
            <TextView android:id="@+id/tv_utils"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:gravity="center"
                android:text="工具" android:textColor="#ffffffff" />
            <TextView android:id="@+id/tv_set"
                android:layout_width="0dp" android:layout_height="wrap_content"
                android:layout_weight="1" android:gravity="center"
                android:text="設置" android:textColor="#ffffffff" />
        </LinearLayout>

        <!-- 選項卡滑動時的游標動畫層 -->
        <LinearLayout
            android:id="@+id/layout_anim"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/menu"
            android:background="#eeff8c00" >
            <ImageView
                android:id="@+id/iv_cursor"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:scaleType="matrix"
                android:src="@drawable/img_cursor" />
        </LinearLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/myPager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/layout_anim"
            android:flipInterval="30"
            android:persistentDrawingCache="animation" />
    </RelativeLayout>
</RelativeLayout>

幾個值得注意的設定:iv_cursor 使用 scaleType="matrix",才能用矩陣平移做出跟手動畫;ViewPager 加上 persistentDrawingCache="animation" 與 flipInterval,切換選項卡時的 redraw 會更順。

二、ViewPager 的三個選項卡

view_main.xml、view_utils.xml、view_set.xml 三者結構一致,都是一個 GridView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#77ff0000"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/myGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="4"
        android:layout_margin="10dp"
        android:horizontalSpacing="20dp"
        android:gravity="center"
        android:verticalSpacing="20dp" >
    </GridView>
</LinearLayout>

numColumns="4" 讓每頁固定四欄,搭配 horizontalSpacing 與 verticalSpacing 控制間距,不必自己算每個格子的寬度。

三、GridView 的項目配置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <ImageView
        android:id="@+id/imgbtn_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/imgbtn_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgbtn_img"
        android:layout_centerHorizontal="true"
        android:text="Text"
        android:textColor="#ff0000ff"
        android:textSize="10sp" />
</RelativeLayout>

四、後續要補的部分

版面配置到此完成,接下來在 Java 端要做的事:

  1. 為三個 GridView 各準備一份資料(圖示資源 + 文字),用 BaseAdapter 綁定上面那個項目配置。
  2. 為 ViewPager 準備 PagerAdapter,把三個 GridView 作為三頁加入。
  3. 在 ViewPager 的 OnPageChangeListener 中,依 position 用 ObjectAnimator.ofFloat(ivCursor, "translationX", ...) 移動游標,並同步切換標籤文字的狀態。
  4. 點擊標籤文字時呼叫 myPager.setCurrentItem(index),讓點擊與滑動兩個方向都能互相驅動。
  5. 最外層的 layout_menu 用 ObjectAnimator 搭配 translationY 做進出場動畫,完成底部菜單的展開與收起。

整體思路就是「ViewPager 負責分頁、GridView 負責排列、游標 ImageView 負責視覺回饋」,三者透過同一個 position 狀態串起來,擴充頁數時只要加資料與對應的標籤即可。

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