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

自定義底部菜單欄的使用教學:引用依賴庫並完成頁面切換

編輯:關於 Android 開發

自定義底部菜單欄的使用教學:引用依賴庫並完成頁面切換

本文接續上一篇文章,示範如何在自己的專案中引用自定義的底部菜單框架,並完成 Fragment 之間的頁面切換。

步驟一:引用依賴庫

右鍵當前工程 → Build Path → Configure Build Path → Android,點擊 Add,選擇你的庫工程並雙擊確認即可。

步驟二:撰寫使用程式碼

在 Activity 中初始化底部菜單資料、綁定點擊回調,並透過 Fragment 切換頁面內容:

package com.example.bluetooth;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Window;
import android.widget.LinearLayout;

import com.weight.tabbottomlib.view.Item;
import com.weight.tabbottomlib.view.MyBottomLayout;
import com.weight.tabbottomlib.view.MyBottomLayout.ICallbackLister;

public class MainActivity extends Activity {
    MyBottomLayout bot;
    ArrayList<Item> itemData = new ArrayList<Item>();
    LinearLayout myContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        findId();
        initData();
        setPageContent(new HomeFragment());
        initclick();
    }

    // 設置頁面內容
    public void setPageContent(Fragment fragment) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.context, fragment);
        ft.commit();
    }

    // 初始化底部菜單欄數據
    private void initData() {
        for (int i = 0; i < 3; i++) {
            switch (i) {
                case 0:
                    itemData.add(new Item(R.drawable.bottom_fp_nor, R.drawable.bottom_fp_pre, "首頁"));
                    break;
                case 1:
                    itemData.add(new Item(R.drawable.bottom_ph_nor, R.drawable.bottom_ph_pre, "陪護師"));
                    break;
                case 2:
                    itemData.add(new Item(R.drawable.bottom_bbs_nor, R.drawable.bottom_bbs_pre, "陪護時光"));
                    break;
            }
        }
        bot.addBottomLayoutValue(itemData);
    }

    private void findId() {
        bot = (MyBottomLayout) findViewById(R.id.bottom);
        myContent = (LinearLayout) findViewById(R.id.context);
    }

    // 初始化點擊事件回調
    private void initclick() {
        bot.setOnCallbackLister(new ICallbackLister() {
            @Override
            public void click(int id) {
                switch (id) {
                    case 0:
                        setPageContent(new HomeFragment());
                        break;
                    case 1:
                        setPageContent(new EscortTeacher());
                        break;
                    case 2:
                        setPageContent(new EscortTime());
                        break;
                }
            }
        });
    }
}

步驟三:撰寫布局檔

在布局中寫入自定義控件的完整包名 + 類名,並預留一個容器給 Fragment 顯示內容:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.weight.tabbottomlib.view.MyBottomLayout
        android:id="@+id/bottom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#9000" >
    </com.weight.tabbottomlib.view.MyBottomLayout>

    <LinearLayout
        android:id="@+id/context"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/bottom"
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>

運行效果

這個框架相當易用,底部頁面的切換也已完整實現。

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