編輯:關於 Android 程式設計
系統原生的 TabWidget 樣式比較制式,很多時候我們想要的是一條「自訂圖示 + 文字」的底部導覽列,還能在最右側點出一個彈出式功能選單。這篇在網路上既有範例的基礎上做了一些調整,把 TabHost、RadioGroup 與 PopupWindow 三者串起來,記錄成一份可重現的筆記。
這個組合的關鍵在於「分工」:
因為自訂了底部列,我們必須用 setCurrentTabByTag() 讓 RadioButton 的選擇狀態與 TabHost 的實際頁面同步,這是整份程式碼的樞紐。
直接寫死像素在不同 dpi 的裝置上會嚴重走形。這裡以 160dpi 為基準密度,用 DisplayMetrics 算出當前裝置的比例,再乘上設計尺寸:
/** 基準螢幕密度 */
public double STANDARD_DENSITY = 160;
private double getDensityRatio() {
DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(displayMetrics);
int currentDensity = displayMetrics.densityDpi;
return currentDensity / STANDARD_DENSITY;
}
以現代的觀點看,更簡潔的做法是直接用 dp 單位(在 XML 裡寫 dip,在程式碼裡用 TypedValue.applyDimension() 轉換),系統會自動處理密度差異,不必自己算比例。
package com.andyidea.tabdemo;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.Toast;
public class MainTabActivity extends TabActivity
implements OnCheckedChangeListener, OnClickListener {
private TabHost mTabHost;
private Intent mAIntent, mBIntent, mCIntent, mDIntent, mEIntent;
private PopupWindow mPopupWindow;
private RadioButton mR1, mR2, mR3, mR4;
private LinearLayout mLinearLayout1, mLinearLayout2, mLinearLayout3,
mLinearLayout4, mLinearLayout5;
/** 基準螢幕密度 */
public double STANDARD_DENSITY = 160;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintabs);
this.mAIntent = new Intent(this, AActivity.class);
this.mBIntent = new Intent(this, BActivity.class);
this.mCIntent = new Intent(this, CActivity.class);
this.mDIntent = new Intent(this, DActivity.class);
this.mEIntent = new Intent(this, EActivity.class);
mR1 = (RadioButton) findViewById(R.id.radio_button0);
mR1.setOnCheckedChangeListener(this);
mR2 = (RadioButton) findViewById(R.id.radio_button1);
mR2.setOnCheckedChangeListener(this);
mR3 = (RadioButton) findViewById(R.id.radio_button2);
mR3.setOnCheckedChangeListener(this);
mR4 = (RadioButton) findViewById(R.id.radio_button3);
mR4.setOnCheckedChangeListener(this);
setupIntent();
initPopupMenu();
}
/** 建立彈出式功能選單 */
private void initPopupMenu() {
View mPopupMenu = LayoutInflater.from(this).inflate(R.layout.popupmenu, null);
mPopupWindow = new PopupWindow(mPopupMenu,
(int) (getDensityRatio() * 90),
(int) (getDensityRatio() * 180));
mLinearLayout1 = (LinearLayout) mPopupMenu.findViewById(R.id.zhanghao);
mLinearLayout2 = (LinearLayout) mPopupMenu.findViewById(R.id.firav);
mLinearLayout3 = (LinearLayout) mPopupMenu.findViewById(R.id.yijian);
mLinearLayout4 = (LinearLayout) mPopupMenu.findViewById(R.id.abort);
mLinearLayout5 = (LinearLayout) mPopupMenu.findViewById(R.id.update);
mLinearLayout1.setOnClickListener(this);
mLinearLayout2.setOnClickListener(this);
mLinearLayout3.setOnClickListener(this);
mLinearLayout4.setOnClickListener(this);
mLinearLayout5.setOnClickListener(this);
}
private void dissmissPopupWindows() {
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
private double getDensityRatio() {
DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(displayMetrics);
int currentDensity = displayMetrics.densityDpi;
return currentDensity / STANDARD_DENSITY;
}
/** 點擊底部列:前三個切頁,第四個彈出選單 */
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
return;
}
dissmissPopupWindows();
switch (buttonView.getId()) {
case R.id.radio_button0:
this.mTabHost.setCurrentTabByTag("A_TAB");
break;
case R.id.radio_button1:
this.mTabHost.setCurrentTabByTag("B_TAB");
break;
case R.id.radio_button2:
this.mTabHost.setCurrentTabByTag("C_TAB");
break;
case R.id.radio_button3:
mPopupWindow.showAsDropDown(mR4, 0, 0);
break;
}
}
/** 把各分頁掛進 TabHost */
private void setupIntent() {
this.mTabHost = getTabHost();
TabHost localTabHost = this.mTabHost;
localTabHost.addTab(buildTabSpec("A_TAB", R.string.main_news, R.drawable.icon_1_n, this.mAIntent));
localTabHost.addTab(buildTabSpec("B_TAB", R.string.main_down, R.drawable.icon_2_n, this.mBIntent));
localTabHost.addTab(buildTabSpec("C_TAB", R.string.main_message, R.drawable.icon_3_n, this.mCIntent));
localTabHost.addTab(buildTabSpec("D_TAB", R.string.more, R.drawable.icon_4_n, this.mDIntent));
}
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
return this.mTabHost
.newTabSpec(tag)
.setIndicator(getString(resLabel), getResources().getDrawable(resIcon))
.setContent(content);
}
/** 彈出選單各項的反應 */
@Override
public void onClick(View v) {
dissmissPopupWindows();
switch (v.getId()) {
case R.id.zhanghao:
Toast.makeText(MainTabActivity.this, "帳號管理", Toast.LENGTH_SHORT).show();
break;
case R.id.firav:
Toast.makeText(MainTabActivity.this, "收藏夾", Toast.LENGTH_SHORT).show();
break;
case R.id.yijian:
Toast.makeText(MainTabActivity.this, "意見反饋", Toast.LENGTH_SHORT).show();
break;
case R.id.abort:
Toast.makeText(MainTabActivity.this, "關於我們", Toast.LENGTH_SHORT).show();
break;
case R.id.update:
Toast.makeText(MainTabActivity.this, "版本更新", Toast.LENGTH_SHORT).show();
break;
}
}
}
每次 onCheckedChanged 與 onClick 的開頭都先呼叫 dissmissPopupWindows()。如果使用者連點幾下,或旋轉螢幕後又回來,未關閉的 PopupWindow 會殘留在畫面上,甚至造成 window leak。更嚴謹的做法是在 onDestroy() 裡也補一次 dismiss。
setCurrentTabByTag("A_TAB") 比 setCurrentTab(0) 更耐修改——日後調整分頁順序時,寫死的 index 很容易錯位,tag 名稱則是語意化的。
四個分頁的組裝邏輯完全相同,抽成一個帶參數的方法之後,新增分頁只要多一行 addTab,不必複製整段鏈式呼叫。
TabActivity 已被標示為 deprecated,官方建議改用 FragmentActivity 搭配 Fragment 來實作分頁。如果你是在維護舊專案,理解這套 RadioGroup 驅動 TabHost 的思路仍然有用;若是新專案,同樣的視覺效果可以用 BottomNavigationView 或 TabLayout + ViewPager2 更省力地做出來,而且能免掉自己處理密度換算與狀態同步的麻煩。
基於 Android 系統的加密檔案系統設計探討
Android 的開放性使其快速發展並為使用者帶來便利,但相對地也讓個人資訊安全面臨風險。個私密檔案隨時可能被窺視或竊取,商務人士與公眾人物尤其關注這個問題。 網路上最常
Android 自訂 View 打造多圖選擇器(支援預覽、刪除、拍照)
做過社群或電商類 App 的人,大概都被「多圖上傳」這個需求折騰過。最經典的參考對象就是微信的圖片選擇器:九宮格排列、可勾選、可預覽、可刪除,還能直接叫相機。與其每次重刻
百度影片怎麼加入收藏?把想看的劇集存起來隨時續看
百度影片不只是一個播放器,它同時具備搜尋與推薦能力,還能離線觀看、提醒劇集更新。其中最實用的其實是最基本的那個:收藏。看到想看的劇,先存起來,之後不必再搜一次。收藏影片的
Android ListView 字母排序:實現字母擠壓、右側快速定位與關鍵字搜索
本文將講解如何自定義 EditText 實現搜索框的自定義樣式,以及字母擠壓效果的實現思路,並最終完成一個具備字母排序、右側快速選取與關鍵字搜索功能的通訊錄列表。 自定義