先看效果
其實做完後,才發現,TabActivity 並不難用,只需要你自己去擴充一些他的方法,就可以達到你自己想到效果。 不多說了,把實現動畫的部分貼出現,其他的自己看源碼吧。
@Overridepublic void setCurrentTab(int index) { int mCurrentTabID = getCurrentTab(); if (null != getCurrentView()) { // 第一次設定 Tab 時,該值為 null。 if (isOpenAnimation) { if (mCurrentTabID == (mTabCount - 1) && index == 0) { getCurrentView().startAnimation(slideLeftOut); } else if (mCurrentTabID == 0 && index == (mTabCount - 1)) { getCurrentView().startAnimation(slideRightOut); } else if (index > mCurrentTabID) { getCurrentView().startAnimation(slideLeftOut); } else if (index < mCurrentTabID) { getCurrentView().startAnimation(slideRightOut); } } } super.setCurrentTab(index); if (isOpenAnimation) { if (mCurrentTabID == (mTabCount - 1) && index == 0) { getCurrentView().startAnimation(slideLeftIn); } else if (mCurrentTabID == 0 && index == (mTabCount - 1)) { getCurrentView().startAnimation(slideRightIn); } else if (index > mCurrentTabID) { getCurrentView().startAnimation(slideLeftIn); } else if (index < mCurrentTabID) { getCurrentView().startAnimation(slideRightIn); } }}
這個程式是Android2.2的。
不過是繼承了 TabHost 組件類,並擴充了其 setCurrentTab(int index) 方法,不過有一個 Bug 沒有解決,便當連續快速的滑動螢幕時,當 TabHost 載入的 view 或 activity 背景圖為透明效果時,會出現重影現象。
希望有高手解決。
關於標籤置底,其實可以查看 XML 檔案得到答案,使用 TabActivity 時,其布局檔案的頂級視圖必須為 TabHost 控制項,通過看 TabHost 的源碼,可以看到,它其實就是一個 FrameLayout,包含了兩個控制項:FrameLayout mTabContent(展示我們載入的 View 或 Activity) 和 TabWidget mTabWidget(展示 Tab 的標籤,其實就是一個 LinearLayout),預設布局都是採用系統的,所以我們可以在自己的
XML 檔案中,將這兩個的順序更換一下,就可以了,同樣的,既然我們可以得到 TabWidget,那麼就可以對其進行布局設計,制定效果也就可以實現了。
現在想來,QQ、UC這些漂亮的按鈕滑動方式,會不會也是這樣實現的呢,研究中…………
完整代碼:http://download.csdn.net/detail/cpcpc/3600134