編輯:關於 Android 程式設計
新聞類客戶端幾乎都看過這種頂部導航:橫向排列的分類,滑一下就換一組。網路上相關範例不少,但多數用 TabHost 實作,能做到背景平滑移動,卻少了「選單本身可左右滑動」這塊。本文提供兩種可行方案。

原理是把 android-support-v4.jar 中的 ViewPager 拿來當容器,每頁放一個橫向排列的 LinearLayout,裡面裝若干 TextView 並設定各自的參數與事件。ViewPager 既能做全螢幕滑動,也能做這種局部滑動。
先建立專案結構:

package com.slide.util;
/** 滑動選單選項類 */
public class SlideMenuUtil {
public static String ITEM_MOBILE = "移動";
public static String ITEM_WEB = "Web";
public static String ITEM_CLOUD = "雲計算";
public static String ITEM_DATABASE = "數據庫";
public static String ITEM_EMBED = "嵌入式";
public static String ITEM_SERVER = "服務器";
public static String ITEM_DOTNET = ".NET";
public static String ITEM_JAVA = "JAVA";
public static String ITEM_SAFE = "安全";
public static String ITEM_DOMAIN = "業界";
public static String ITEM_RESEASRCH = "研發";
public static String ITEM_MANAGE = "管理";
// 選單項計數器
public int count = 0;
}
左右兩顆箭頭要疊在文字上方,因此外層使用 RelativeLayout:

<RelativeLayout android:id="@+id/linearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v4.view.ViewPager
android:id="@+id/slideMenu"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="@drawable/top_bg" />
<RelativeLayout android:id="@+id/linearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/ivPreviousButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:visibility="invisible"
android:src="@drawable/previous_button" />
</RelativeLayout>
<RelativeLayout android:id="@+id/linearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/ivNextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:paddingTop="10dp"
android:paddingRight="5dp"
android:visibility="invisible"
android:src="@drawable/next_button" />
</RelativeLayout>
<ImageView android:id="@+id/ivMenuBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:layout_toRightOf="@+id/ivPreviousButton"
android:paddingTop="2dp"
android:visibility="gone"
android:src="@drawable/menu_bg" />
</RelativeLayout>
其中 ivMenuBackground 是點選項目後用來標示選擇狀態的背景圖,預設選中第一項「移動」。另外,item_xxx.xml 這類版面檔就是各分類對應的內容區,範例中只是佔位,可自行替換。
private String[][] menus = {
{SlideMenuUtil.ITEM_MOBILE, SlideMenuUtil.ITEM_WEB, SlideMenuUtil.ITEM_CLOUD, SlideMenuUtil.ITEM_DATABASE},
{SlideMenuUtil.ITEM_EMBED, SlideMenuUtil.ITEM_SERVER, SlideMenuUtil.ITEM_DOTNET, SlideMenuUtil.ITEM_JAVA},
{SlideMenuUtil.ITEM_SAFE, SlideMenuUtil.ITEM_DOMAIN, SlideMenuUtil.ITEM_RESEASRCH, SlideMenuUtil.ITEM_MANAGE}
};
第一維決定有幾頁,第二維決定每頁有幾個項目。初始化時把每一頁都先備好:
LayoutInflater inflater = getLayoutInflater();
menuViews = new ArrayList<View>();
SlideMenuLayout menu = new SlideMenuLayout(this);
for (int i = 0; i < menus.length; i++) {
menuViews.add(menu.getSlideMenuLinerLayout(menus[i], screenWidth));
}
main = (ViewGroup) inflater.inflate(R.layout.main, null);
menuViews 用來裝各頁的 View,三頁就是三項;screenWidth 是螢幕寬度。核心方法是 SlideMenuLayout 的 getSlideMenuLinerLayout:
/**
* 頂部滑動選單版面
* @param menuTextViews 每頁的選單項目
* @param layoutWidth 螢幕寬度
*/
public View getSlideMenuLinerLayout(String[] menuTextViews, int layoutWidth) {
LinearLayout menuLinerLayout = new LinearLayout(activity);
menuLinerLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams menuLinerLayoutParames = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
menuLinerLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;
for (int i = 0; i < menuTextViews.length; i++) {
TextView tvMenu = new TextView(activity);
tvMenu.setTag(menuTextViews[i]);
tvMenu.setLayoutParams(new LayoutParams(layoutWidth / 4, 30));
tvMenu.setPadding(30, 14, 30, 10);
tvMenu.setText(menuTextViews[i]);
tvMenu.setTextColor(Color.WHITE);
tvMenu.setGravity(Gravity.CENTER_HORIZONTAL);
tvMenu.setOnClickListener(SlideMenuOnClickListener);
menuUtil.count++;
// 第一個項目預設套用背景
if (menuUtil.count == 1) {
tvMenu.setBackgroundResource(R.drawable.menu_bg);
}
menuLinerLayout.addView(tvMenu, menuLinerLayoutParames);
menuList.add(tvMenu);
}
return menuLinerLayout;
}
OnClickListener SlideMenuOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
String menuTag = v.getTag().toString();
if (v.isClickable()) {
textView = (TextView) v;
textView.setBackgroundResource(R.drawable.menu_bg);
// 清掉其他項目的背景
for (int i = 0; i < menuList.size(); i++) {
if (!menuTag.equals(menuList.get(i).getText())) {
menuList.get(i).setBackgroundDrawable(null);
}
}
// 切換下方內容
slideMenuOnChange(menuTag);
}
}
};
private void slideMenuOnChange(String menuTag) {
LayoutInflater inflater = activity.getLayoutInflater();
ViewGroup llc = (ViewGroup) activity.findViewById(R.id.linearLayoutContent);
llc.removeAllViews();
if (menuTag.equals(SlideMenuUtil.ITEM_MOBILE)) {
llc.addView(inflater.inflate(R.layout.item_mobile, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_WEB)) {
llc.addView(inflater.inflate(R.layout.item_web, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_CLOUD)) {
llc.addView(inflater.inflate(R.layout.item_cloud, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_DATABASE)) {
llc.addView(inflater.inflate(R.layout.item_database, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_EMBED)) {
llc.addView(inflater.inflate(R.layout.item_embed, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_SERVER)) {
llc.addView(inflater.inflate(R.layout.item_server, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_DOTNET)) {
llc.addView(inflater.inflate(R.layout.item_dotnet, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_JAVA)) {
llc.addView(inflater.inflate(R.layout.item_java, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_SAFE)) {
llc.addView(inflater.inflate(R.layout.item_safe, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_DOMAIN)) {
llc.addView(inflater.inflate(R.layout.item_domain, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_RESEASRCH)) {
llc.addView(inflater.inflate(R.layout.item_research, null));
} else if (menuTag.equals(SlideMenuUtil.ITEM_MANAGE)) {
llc.addView(inflater.inflate(R.layout.item_manage, null));
}
}
在 ViewPager 的 onPageSelected 中依目前頁碼決定兩顆箭頭是否出現:
@Override
public void onPageSelected(int arg0) {
int pageCount = menuViews.size() - 1;
pagerIndex = arg0;
// 還沒到最後一頁,顯示右箭頭
if (arg0 >= 0 && arg0 < pageCount) {
imageNext.setVisibility(View.VISIBLE);
} else {
imageNext.setVisibility(View.INVISIBLE);
}
// 已不在第一頁,顯示左箭頭
if (arg0 > 0 && arg0 <= pageCount) {
imagePrevious.setVisibility(View.VISIBLE);
} else {
imagePrevious.setVisibility(View.INVISIBLE);
}
}
若確定有多頁,也可以直接讓右箭頭常駐:
if (menuViews.size() > 1) {
imageNext.setVisibility(View.VISIBLE);
}
最後為兩顆箭頭掛上點擊事件,直接跳到上一頁或下一頁:
// 右箭頭
class ImageNextOnclickListener implements OnClickListener {
@Override
public void onClick(View v) {
pagerIndex++;
viewPager.setCurrentItem(pagerIndex);
}
}
// 左箭頭
class ImagePreviousOnclickListener implements OnClickListener {
@Override
public void onClick(View v) {
pagerIndex--;
viewPager.setCurrentItem(pagerIndex);
}
}
上面的做法是整頁滑動,滑到一半放手不會停在原位,而是直接跳到下一頁。如果你想做的是「一點一點往左或往右移動,沒滑過門檻就彈回原頁」,ViewPager 並不適合,得改用 HorizontalScrollView。
這種做法單純許多,只要把選單放進橫向可捲動的容器即可。要注意 HorizontalScrollView 只能有一個直接子節點。

<HorizontalScrollView android:layout_width="match_parent"
android:layout_height="35dp"
android:scrollbars="none">
<LinearLayout android:id="@+id/linearLayoutMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_bg"
android:gravity="center_vertical">
</LinearLayout>
</HorizontalScrollView>
選單項一樣用 TextView,以程式碼動態加入:
private void setSlideMenu() {
LinearLayout menuLinerLayout = (LinearLayout) findViewById(R.id.linearLayoutMenu);
menuLinerLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams menuLinerLayoutParames = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
menuLinerLayoutParames.gravity = Gravity.CENTER_HORIZONTAL;
for (int i = 0; i < menus.length; i++) {
TextView tvMenu = new TextView(this);
tvMenu.setLayoutParams(new LayoutParams(30, 30));
tvMenu.setPadding(30, 14, 30, 10);
tvMenu.setText(menus[i]);
tvMenu.setTextColor(Color.WHITE);
tvMenu.setGravity(Gravity.CENTER_HORIZONTAL);
menuLinerLayout.addView(tvMenu, menuLinerLayoutParames);
}
}
如果需要在選單上方加標題或下方放內容,把 HorizontalScrollView 嵌進其他版面即可。
想要「整頁切換、每頁一組分類」的形態,用 ViewPager;想要「一整條連續滑動、逐格移動」的形態,用 HorizontalScrollView。至於用 Gallery 來做選單滑動也是可行路,但 Gallery 早已不建議使用,此處不再細談。
Android 電話服務實戰:黑名單攔截、通話錄音與 AIDL 掛斷
之前的代碼都是零散片段,這次把電話相關的能力整合成一個完整的服務模塊:來電黑名單攔截、通話錄音、以及通過 AIDL 實現的掛斷能力。整理成文,歡迎交流指正。 先說前提:本
從 Android 源碼到 APK:打包流程一次看懂
打包流程 Android 程式從源碼到可安裝的 APK,通常會經過資源處理、程式碼編譯、DEX 轉換、打包、對齊與簽名等步驟。 在較詳細的流程中,建置工具會先處理 An
Android 圖片裁剪實戰:調用系統裁剪意圖與內存優化
項目裡需要頭像的裁剪與上傳。關於裁剪,一開始想自己寫,轉念一想這種通用功能系統裡大概已經有了,翻官方文檔果然找到了現成方案。時間緊張,就直接用了系統自帶的裁剪 Activ
Android實現圖片中疊加可拖動文字功能
本文為大家分享一個在圖片中疊加文字、並支持拖動改變位置的實例,供感興趣的讀者參考。 之所以做這個Demo,是因為項目裡遇到一個頗為奇葩的需求:用戶拍照後分享到微信,同時希