編輯:Android 開發實例
網路上搜得到的「仿 QQ 空間底部選單」範例不少,但仔細看會發現很多問題:布局裡塞滿了寫死的魔法數字、不同解析度下位置就歪掉、PopupWindow 的生命週期也沒處理好。與其修別人的 Bug,不如自己從原理重新寫一遍。

整個底部選單可以拆成三個獨立問題:
1. 平均分割寬度
最外層是一個水平的 LinearLayout,裡面放五個子 LinearLayout,每個的 layout_weight 都設為 1,系統就會自動把螢幕寬度均分為五等份。這種寫法的好處是完全不用算像素,任何解析度都能正確對齊。四側的子 LinearLayout 各自放一組 ImageView 加 TextView,中間那格先留空。
2. 中央加號與底盤的堆疊
中間格改用 FrameLayout 承載,因為 FrameLayout 的子 View 預設會堆在同一個位置。先放一張底盤圖當背景,再在相同位置放上加號圖,視覺上就是「凸起的圓盤上面有一個加號」。
3. 點擊彈出面板
給加號掛上點擊事件,用 PopupWindow 彈出寫作面板,並在彈出時把加號圖換成乘號,收合時再換回來。
public class ButtomMenuActivity extends TabActivity {
FrameLayout fmpan; // 底部選單容器
FrameLayout fm; // 中央加號的容器
ImageView image; // 加號/乘號圖示
PopupWindow popup;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
initView();
fm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
image.setImageResource(R.drawable.toolbar_plusback); // 加號換成乘號
showWindow(fmpan);
}
});
}
private void initView() {
fmpan = (FrameLayout) findViewById(R.id.tab1);
fm = (FrameLayout) findViewById(R.id.btn_ck);
image = (ImageView) findViewById(R.id.image1);
}
private void showWindow(View parent) {
if (popup == null) {
LayoutInflater layoutInflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.write_tab, null);
popup = new PopupWindow(view,
LinearLayout.LayoutParams.MATCH_PARENT, 320);
popup.setFocusable(true);
popup.setOutsideTouchable(true);
// 關閉時把圖示還原
popup.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
image.setImageResource(R.drawable.toolbar_plus);
}
});
// 必須給一個背景 Drawable,否則點擊外部不會消失
popup.setBackgroundDrawable(new BitmapDrawable());
popup.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popup.dismiss();
image.setImageResource(R.drawable.toolbar_plus);
return true;
}
return false;
}
});
}
if (!popup.isShowing()) {
popup.showAsDropDown(parent, Gravity.CENTER, 0);
}
}
}
這裡有個容易踩到的坑:setBackgroundDrawable() 不能省。若 PopupWindow 沒有背景 Drawable,Android 判定它可以全透明穿透,於是「點擊外部關閉」與返回鍵行為都會失效。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="10.0px"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:layout_alignParentBottom="true"
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/test" />
</RelativeLayout>
</FrameLayout>
</TabHost>
關鍵是把系統自帶的 TabWidget 設為 gone,改用 include 進來的自訂選單,並用 alignParentBottom 釘在畫面底部。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/toolbar_bg" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<!-- 好友動態 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:src="@drawable/tab_timefeed_opacity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="好友動態"
android:textSize="10.0dip" />
</LinearLayout>
<!-- 與我相關 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:src="@drawable/tab_feedback_opacity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="與我相關"
android:textSize="10.0dip" />
</LinearLayout>
<!-- 中間留空,負責撐出五等份 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" />
<!-- 主頁 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:src="@drawable/tab_myzone_opacity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="主頁"
android:textSize="10.0dip" />
</LinearLayout>
<!-- 應用 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:src="@drawable/tab_applist_opacity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="應用"
android:textSize="10.0dip" />
</LinearLayout>
</LinearLayout>
<!-- 底盤 -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/toolbar_write_bg" />
</FrameLayout>
<!-- 加號,疊在底盤上方 -->
<FrameLayout
android:id="@+id/btn_ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1" >
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2.0dip"
android:src="@drawable/toolbar_plus" />
</FrameLayout>
</FrameLayout>
這個範例的重點是把原理講清楚,因此仍有些地方可以再打磨:
include 或 merge 搭配 style),避免大量重複的 XML。PopupWindow 建議在 Activity 的 onDestroy 中呼叫 dismiss(),否則螢幕旋轉時容易發生 window leak。以上就是仿 QQ 空間底部選單的完整實作。這個 Demo 純粹是練功性質,寫法未必要照抄,重點是理解「weight 均分 + FrameLayout 堆疊 + PopupWindow 彈出」這三個組合技。若有不清楚的地方,歡迎提出討論。
Android 應用本地化:values-local 與多語系字串設定
Android 的本地化機制主要是透過不同資源資料夾來達成。當系統語言改變時,Android 會優先載入符合目前語言環境的資源。 資料夾命名規則 本地化字串通常放在 re
Android 如何解析 JSON?從 JSONObject 到天氣查詢 App 的完整示範
行動裝置上的 App 要拿到伺服器的資料,回傳格式十之八九是 JSON。JSON 的全名是 JavaScript Object Notation,它與程式語言無關、純文字
Android 多語系支援:strings.xml 與 values-it 目錄
Android 的資源系統會根據裝置語言自動選擇合適的字串資源。只要將不同語言的 strings.xml 放在對應的 values 目錄,App 就能依系統設定顯示正確語
用 Android 打造漫天飛雪與下雨天的動態效果
前言這套生動的降雪與降雨動畫,最初靈感來自國外一位開發者的巧思。我在他的基礎上反覆調試,為程式碼補上大量註解,並對部分細節做了修改。在此之上,我沿用漫天飛雪的核心思路,進