編輯:關於 Android 程式設計
導航幾乎是所有應用的入口。Android 提供了多套實現思路,從早期的 TabActivity 到後來的 ViewPager 加 Fragment,各有適用場景。本文把五種常見方案整理一遍,並附上關鍵代碼。
最傳統的做法是讓 Activity 繼承 TabActivity,通過 getTabHost() 拿到容器,再逐一添加 Tab。每個 Tab 的內容由一個 Intent 指定,也就是說每個頁簽都是一個獨立的 Activity。
public class HomeActivity extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initViews();
}
private void initViews() {
mTabHost = getTabHost(); // 獲取最外層的容器
Intent intent1 = new Intent(this, OftenUseFunctionActivity.class);
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setIndicator("常用功能", getResources().getDrawable(R.drawable.accelerate_selector))
.setContent(intent1));
Intent intent2 = new Intent(this, AppManagerActivity.class);
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("軟件管理", getResources().getDrawable(R.drawable.soft_manager_selector))
.setContent(intent2));
Intent intent3 = new Intent(this, SafeProtectActivity.class);
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator("安全防護", getResources().getDrawable(R.drawable.security_selector))
.setContent(intent3));
Intent intent4 = new Intent(this, PrivateProtectActivity.class);
mTabHost.addTab(mTabHost.newTabSpec("tab4")
.setIndicator("隱私保護", getResources().getDrawable(R.drawable.profile_selector))
.setContent(intent4));
}
}
布局必須包含 TabHost、TabWidget 和 FrameLayout 三部分,且 id 要使用系統預留的 @android:id/tabhost、@android:id/tabs、@android:id/tabcontent:
<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"
android:background="#ffffff" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget
android:layout_alignParentBottom="true"
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:background="#11ff52"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:layout_marginBottom="60dip"
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
</RelativeLayout>
</TabHost>


這種方式的問題也很明顯:TabActivity 已被標記為 Deprecated;每個 Tab 都是獨立 Activity,內存開銷大;Tab 之間無法共享狀態;而且不支持滑動切換。
如果想保留 TabHost 的外觀但擺脫繼承限制,可以在布局中直接聲明 TabHost,代碼裡手動 setup(),內容直接指向布局中的 View 而非 Activity:
/**
* 初始化 UI 組件
*/
private void initViews() {
setContentView(R.layout.activity_home);
mTabHost = (TabHost) this.findViewById(R.id.tabhost);
mTabHost.setup(); // 關鍵:手動初始化,否則會空指針
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setIndicator("第一個", getResources().getDrawable(R.drawable.unresloved_selector))
.setContent(R.id.lv_leave_infos));
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("第二個", getResources().getDrawable(R.drawable.leave_apply_selector))
.setContent(R.id.ll_tb_leave_apply));
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator("第三個", getResources().getDrawable(R.drawable.exception_note_selector))
.setContent(R.id.ll_tb_leave_excepiton_note));
mTabHost.setCurrentTabByTag("tab1");
}
對應布局把所有頁面的內容都放進同一個 FrameLayout 中:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- 第一個 -->
<LinearLayout
android:id="@+id/ll_tb_unresolve"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_leave_infos"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- 第二個 -->
<LinearLayout
android:layout_marginTop="45dp"
android:id="@+id/ll_tb_leave_apply"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_leave_info_accept"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- 第三個 -->
<LinearLayout
android:layout_marginTop="45dp"
android:id="@+id/ll_tb_leave_excepiton_note"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_leave_info_reject"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</TabHost>
切換時通過監聽來按需加載數據,這是一個很實用的技巧——只加載當前可見頁的內容,避免啟動時一次性查三次數據庫:
/**
* 設置監聽
*/
private void setListener() {
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) {
fillData();
} else if (tabId.equals("tab2")) {
mLVleaveInfosAccept = (ListView) findViewById(R.id.lv_leave_info_accept);
fillDataAccept();
showDeleteOrNo();
} else if (tabId.equals("tab3")) {
mLVleaveInfoReject = (ListView) findViewById(R.id.lv_leave_info_reject);
fillDataReject();
showDeleteOrNoReject();
}
}
});
}
這是曾經最主流的方案:用 ViewPager 承載各頁 View,底部放一排 ImageView 作為標籤,再用一個 ImageView 配合 TranslateAnimation 做出「滑動指示條」的效果。
public class MainWeixin extends Activity {
public static MainWeixin instance = null;
private ViewPager mTabPager;
private ImageView mTabImg; // 動畫圖片
private ImageView mTab1, mTab2, mTab3, mTab4;
private int zero = 0; // 動畫圖片偏移量
private int currIndex = 0; // 當前頁卡編號
private int one, two, three; // 單頁、兩頁、三頁的位移量
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_weixin);
// 啟動時不自動彈出軟鍵盤
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
instance = this;
mTabPager = (ViewPager) findViewById(R.id.tabpager);
mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());
mTab1 = (ImageView) findViewById(R.id.img_weixin);
mTab2 = (ImageView) findViewById(R.id.img_address);
mTab3 = (ImageView) findViewById(R.id.img_friends);
mTab4 = (ImageView) findViewById(R.id.img_settings);
mTabImg = (ImageView) findViewById(R.id.img_tab_now);
mTab1.setOnClickListener(new MyOnClickListener(0));
mTab2.setOnClickListener(new MyOnClickListener(1));
mTab3.setOnClickListener(new MyOnClickListener(2));
mTab4.setOnClickListener(new MyOnClickListener(3));
// 根據屏幕寬度計算每屏的位移量
Display currDisplay = getWindowManager().getDefaultDisplay();
int displayWidth = currDisplay.getWidth();
one = displayWidth / 4;
two = one * 2;
three = one * 3;
// 加載四個頁面
LayoutInflater mLi = LayoutInflater.from(this);
View view1 = mLi.inflate(R.layout.main_tab_weixin, null);
View view2 = mLi.inflate(R.layout.main_tab_address, null);
View view3 = mLi.inflate(R.layout.main_tab_friends, null);
View view4 = mLi.inflate(R.layout.main_tab_settings, null);
final ArrayList<View> views = new ArrayList<View>();
views.add(view1);
views.add(view2);
views.add(view3);
views.add(view4);
// 填充 ViewPager 的數據適配器
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return views.size();
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(views.get(position));
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(views.get(position));
return views.get(position);
}
};
mTabPager.setAdapter(mPagerAdapter);
}
/** 標題點擊監聽 */
public class MyOnClickListener implements View.OnClickListener {
private int index = 0;
public MyOnClickListener(int i) { index = i; }
@Override
public void onClick(View v) { mTabPager.setCurrentItem(index); }
}
/** 頁卡切換監聽 */
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:
mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_pressed));
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, 0, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
}
break;
case 1:
mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, one, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, one, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
}
break;
case 2:
mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, two, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, two, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_normal));
}
break;
case 3:
mTab4.setImageDrawable(getResources().getDrawable(R.drawable.tab_settings_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, three, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, three, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, three, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(R.drawable.tab_find_frd_normal));
}
break;
}
currIndex = arg0;
animation.setFillAfter(true); // 停在動畫結束位置
animation.setDuration(150);
mTabImg.startAnimation(animation);
}
@Override public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override public void onPageScrollStateChanged(int arg0) { }
}
}
這裡的 switch-case 寫法很直觀,但也最繁瑣——四個頁面就要列舉 12 種切換組合,頁面更多時完全不可維護。後面方案五的做法(用 positionOffset 連續計算 leftMargin)優雅得多。
菜單鍵的處理也順帶示範了 PopupWindow 的用法:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
if (menu_display) {
menuWindow.dismiss();
menu_display = false;
} else {
startActivity(new Intent(MainWeixin.this, Exit.class));
}
} else if (keyCode == KeyEvent.KEYCODE_MENU) {
if (!menu_display) {
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.main_menu, null);
menuWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
menuWindow.showAtLocation(this.findViewById(R.id.mainweixin),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
mClose = (LinearLayout) layout.findViewById(R.id.menu_close);
mCloseBtn = (LinearLayout) layout.findViewById(R.id.menu_close_btn);
mCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(MainWeixin.this, Exit.class));
menuWindow.dismiss();
}
});
menu_display = true;
} else {
menuWindow.dismiss();
menu_display = false;
}
}
return false;
}
底部導航的布局要點是:指示條 ImageView 用 scaleType="matrix" 並對齊底部,四個標籤用權重均分,這樣才能實現「圖片滑動、文字不動」的效果。
如果項目已經用了 TabActivity 又實在想要滑動效果,可以借助 GestureDetector 監聽左右滑動手勢,再把 setCurrentTab() 換成對應的頁簽:
public class StopAnonyActivity extends TabActivity {
private TabHost mTabHost;
private static final int SWIPE_MIN_DISTANCE = 120; // 最小滑動距離
private static final int SWIPE_MAX_OFF_PATH = 250; // 最大偏移
private static final int SWIPE_THRESHOLD_VELOCITY = 200; // 速度閾值
private GestureDetector gestureDetector;
int currentView = 0;
private static int maxTabIndex = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
gestureDetector = new GestureDetector(new MyGestureDetector());
}
private void init() {
setContentView(R.layout.activity_stop_annoy);
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab1")
.setIndicator("垃圾短信").setContent(RubishSmsActivity.class));
mTabHost.addTab(mTabHost.newTabSpec("tab2")
.setIndicator("騷擾電話").setContent(AnnoyCallActivity.class));
mTabHost.addTab(mTabHost.newTabSpec("tab3")
.setIndicator("攔截設置").setContent(StopAnnoySettingActivity.class));
}
// 左右滑動切換頁簽
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
TabHost tabHost = getTabHost();
try {
// 縱向偏移太大說明不是橫向滑動,直接放棄
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// 從右往左滑,下一頁
currentView = (currentView == maxTabIndex) ? 0 : currentView + 1;
tabHost.setCurrentTab(currentView);
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// 從左往右滑,上一頁
currentView = (currentView == 0) ? maxTabIndex : currentView - 1;
tabHost.setCurrentTab(currentView);
}
} catch (Exception e) { }
return false;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
}

這裡的三個閾值判斷值得借鑒:最小距離過濾誤觸,最大縱向偏移確保是橫向手勢,速度閾值讓「快速小幅滑動」也能生效。缺少任何一項,體驗都會打折。
這是目前最清晰的實現方式。每個頁簽對應一個 Fragment,ViewPager 通過 FragmentPagerAdapter 管理,指示線則根據 positionOffset 連續計算位置——不再需要列舉所有切換組合。
public class MainActivity extends FragmentActivity {
private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List<Fragment> mFragments = new ArrayList<Fragment>();
private LinearLayout mTabLiaotian, mTabFaxian, mTabTongxunlun;
private TextView mLiaotian, mFaxian, mTongxunlu;
private BadgeView mBadgeViewforLiaotian, mBadgeViewforFaxian, mBadgeViewforTongxunlu;
private ImageView mTabLine; // 底部引導線
private int currentIndex; // ViewPager 當前頁
private int screenWidth; // 屏幕寬度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
initView();
initTabLine();
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int arg0) {
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
resetTextView(); // 先重置所有標籤顏色
switch (position) {
case 0:
mTabLiaotian.removeView(mBadgeViewforLiaotian);
mBadgeViewforLiaotian.setBadgeCount(5);
mTabLiaotian.addView(mBadgeViewforLiaotian);
mLiaotian.setTextColor(getResources().getColor(R.color.green));
break;
case 1:
mFaxian.setTextColor(getResources().getColor(R.color.green));
mTabFaxian.removeView(mBadgeViewforFaxian);
mBadgeViewforFaxian.setBadgeCount(15);
mTabFaxian.addView(mBadgeViewforFaxian);
break;
case 2:
mTongxunlu.setTextColor(getResources().getColor(R.color.green));
break;
}
currentIndex = position;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// 依據 position 與 currentIndex 判斷滑動方向,
// 再用 positionOffset 連續改變 leftMargin,實現跟手效果
if (currentIndex == 0 && position == 0) { // 0 -> 1
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)
mTabLine.getLayoutParams();
lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3)
+ currentIndex * (screenWidth / 3));
mTabLine.setLayoutParams(lp);
} else if (currentIndex == 1 && position == 0) { // 1 -> 0
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)
mTabLine.getLayoutParams();
lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3)
+ currentIndex * (screenWidth / 3));
mTabLine.setLayoutParams(lp);
} else if (currentIndex == 1 && position == 1) { // 1 -> 2
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)
mTabLine.getLayoutParams();
lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 3)
+ currentIndex * (screenWidth / 3));
mTabLine.setLayoutParams(lp);
} else if (currentIndex == 2 && position == 1) { // 2 -> 1
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)
mTabLine.getLayoutParams();
lp.leftMargin = (int) (-(1 - positionOffset) * (screenWidth * 1.0 / 3)
+ currentIndex * (screenWidth / 3));
mTabLine.setLayoutParams(lp);
}
}
@Override
public void onPageScrollStateChanged(int state) { }
});
mViewPager.setCurrentItem(1);
}
/** 根據屏幕寬度初始化引導線寬度為三分之一屏 */
private void initTabLine() {
mTabLine = (ImageView) findViewById(R.id.id_tab_line);
DisplayMetrics outMetrics = new DisplayMetrics();
getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams)
mTabLine.getLayoutParams();
lp.width = screenWidth / 3;
mTabLine.setLayoutParams(lp);
}
protected void resetTextView() {
mLiaotian.setTextColor(getResources().getColor(R.color.black));
mFaxian.setTextColor(getResources().getColor(R.color.black));
mTongxunlu.setTextColor(getResources().getColor(R.color.black));
}
/** 初始化控件與 Fragment */
private void initView() {
mTabLiaotian = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);
mTabFaxian = (LinearLayout) findViewById(R.id.id_tab_faxian_ly);
mTabTongxunlun = (LinearLayout) findViewById(R.id.id_tab_tongxunlu_ly);
mLiaotian = (TextView) findViewById(R.id.id_liaotian);
mFaxian = (TextView) findViewById(R.id.id_faxian);
mTongxunlu = (TextView) findViewById(R.id.id_tongxunlu);
mFragments.add(new MainTab01());
mFragments.add(new MainTab02());
mFragments.add(new MainTab03());
mBadgeViewforLiaotian = new BadgeView(this);
mBadgeViewforFaxian = new BadgeView(this);
mBadgeViewforTongxunlu = new BadgeView(this);
}
}
這個方案相比方案三的優勢很明顯:
setUserVisibleHint,方便按需加載數據;新項目直接上方案五,配合 ViewPager2 與 FragmentStateAdapter 更省事,它內置支持橫向滑動與 RTL 布局。老項目維護時,方案四(手勢 + TabHost)可以在不改變整體架構的前提下改善體驗。而方案一、二僅建議在看到歷史代碼時閱讀理解,新代碼不應再使用。
自製 Android 日曆控制項:月份/週雙模式與無限滑動
無聊時想動手做点小工具,於是花了一天多寫出一個堪用的日曆 View。它支援兩種檢視模式:整月與單週,並可左右滑動翻頁。先看成果:上圖為月份模式;下圖則是週模式:設計思路核
Android 刮刮樂效果實作:自定義擦除視圖
效果原理刮刮樂效果的本質,是在文字或圖案上覆蓋一層可擦除的遮罩。當使用者手指滑過時,遮罩被擦開,底下的內容逐漸顯現。自定義擦除元件可建立一個自訂 TextView,內部使
360 手機 Q5 怎麼插卡?SIM 卡安裝與電信支援說明
360 手機 Q5 配備雙鏡頭、主打高端安全,並支援全網通與 VoLTE 高清通話。新機到手第一步是裝卡,下面把卡型、卡槽與安裝方式說清楚。 裝卡前先了解三件事 網絡制
Android 自訂 View 的入門實作教學
在專案開發中,系統內建的一些 Widget 未必能滿足所有需求,這時就需要自訂 View。由於 Button、TextView、EditText 等常見控制項都繼承自 V