編輯:關於 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>

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

WebView 載入網頁並加入進度條
WebView 載入網頁並加入進度條 在 Android 中內嵌網頁時,常希望像微信一樣,在頂部顯示載入進度。這可以透過 WebView 搭配 ProgressBar 與
Android 二級展開列表:ExpandableListView 實作詳解
打造「列表中的列表」 有時候我們需要這樣的界面:外層是一個列表,點擊某個條目後會展開,出現內嵌的子列表。這正是 ExpandableListView 的用武之地。下圖為程
在 Access 中用 SQL 查詢語法檢查指令:圖文步驟流程
在 Access 中用 SQL 查詢語法檢查指令:圖文步驟流程 寫 SQL 的時候,能在 Access 裡直接跑一遍確認語法,比對著終端機猜錯在哪裡要有效率得多。Acce
Git 版本控制工具(二):本地版本庫的常用操作詳解
Git 版本控制工具(二):本地版本庫的常用操作詳解 在上一章節中,我們學習了 Git 最基本的用法,包括安裝、創建版本庫與提交本地代碼。本章將介紹更多實用技巧。首先建立