編輯:Android 開發實例
做應用的時候有個需求:想把設定頁與主畫面放在一起,讓使用者能一邊調整、一邊即時看到介面變化。想了一陣子,發現 SlidingDrawer 剛好合用——拉開抽屜佔半個螢幕放設定,另外半屏仍是應用內容,效果相當自然。
其實 Android 的 launcher 本身就是一個抽屜:拉開來就是已安裝的應用清單。這個元件官方文件著墨不多,很多人因此不知道它存在,但只要在 xml 裡寫幾行就能用。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="20sp" />
<SlidingDrawer
android:id="@+id/sd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="@+id/iv"
android:content="@+id/myContent"
android:orientation="vertical">
<!-- 把手:使用者拖曳的圖 -->
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/open1" />
<!-- 抽屜內容 -->
<GridView
android:id="@id/myContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:background="@drawable/background"
android:gravity="center" />
</SlidingDrawer>
</RelativeLayout>
兩個關鍵屬性:android:handle 指向把手 View(使用者拖的那張圖),android:content 指向抽屜裡真正的內容。有了這份配置,一個能拉開關閉的抽屜就成立了。
要注意 SlidingDrawer 只能有兩個子 View,順序必須是「把手在前、內容在後」,而 android:orientation 決定抽屜往哪個方向開(vertical 由下往上、horizontal 由左往右)。
抽屜開關時,把手圖示應該要跟著變(例如箭頭朝上/朝下),這靠兩個監聽器完成:
public class Chouti extends Activity {
private GridView gv;
private SlidingDrawer sd;
private ImageView iv;
private int[] icons = { R.drawable.browser, R.drawable.gallery,
R.drawable.camera, R.drawable.gmail, R.drawable.music,
R.drawable.market, R.drawable.phone, R.drawable.messages,
R.drawable.maps };
private String[] items = { "瀏覽器", "圖片", "相機", "時鐘", "音樂",
"市場", "撥號", "資訊", "地圖" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView) findViewById(R.id.myContent);
sd = (SlidingDrawer) findViewById(R.id.sd);
iv = (ImageView) findViewById(R.id.iv);
MyAdapter adapter = new MyAdapter(this, items, icons);
gv.setAdapter(adapter);
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
iv.setImageResource(R.drawable.close1); // 開啟時換成「收起」圖
}
});
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
iv.setImageResource(R.drawable.open1); // 關閉時換回「展開」圖
}
});
}
}
GridView 要顯示「圖在上、字在下」的九宮格,需要一個自訂 Adapter。做法是把自訂版面 inflate 出來,再分別塞入圖片與文字:
public class MyAdapter extends BaseAdapter {
private Context _ct;
private String[] _items;
private int[] _icons;
public MyAdapter(Context ct, String[] items, int[] icons) {
_ct = ct;
_items = items;
_icons = icons;
}
@Override public int getCount() { return _items.length; }
@Override public Object getItem(int arg0) { return _items[arg0]; }
@Override public long getItemId(int position) { return position; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(_ct);
View v = factory.inflate(R.layout.gv, null); // 綁定自訂版面
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tv = (TextView) v.findViewById(R.id.text);
iv.setImageResource(_icons[position]);
tv.setText(_items[position]);
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="40px"
android:layout_gravity="center" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#ffffffff" />
</LinearLayout>
抽屜最適合的场景,就是「內容會互相干擾、但使用者需要同時看到兩者」:設定與預覽、篩選條件與結果列表、遊戲主控台與畫面、地圖與图层控制。相比 Dialog 會遮斷操作,抽屜讓兩側都保持可用,這正是它的價值。
實務上有幾點要留意:SlidingDrawer 不適合放在 ScrollView 裡(拖曳手勢會打架);抽屜內容在關閉時仍會被 inflate,若內容很重會影響啟動效能;另外它在較新的 Android 版本中已被標記為 deprecated,官方建議改用 NavigationDrawer 或 BottomSheet。理解它的原理與手勢處理方式,對實作自訂抽屜還是有幫助的。
Android 模擬定位實作教學:權限宣告、狀態偵測與座標注入
想在 App 裡驗證定位功能,卻不方便真的出門跑一條路線?Android 系統本身內建了「模擬位置」機制:只要在開發者選項中指定一支代理程式,系統 GPS 就會回報你自己
Windows 上架設 Android 開發環境完整指南(JDK、Eclipse、SDK、ADT)
本文寫給第一次想在 Windows 上打造 Android 開發環境的朋友,把整個流程拆成五個步驟,盡量把每個環節講清楚,讓你能一次把環境搭好。 五個步驟分別是: 第一步
Android Spinner 載入進度條實作
在 Android 中,可以透過進度條向使用者顯示某項任務的執行進展。進度條主要分為兩種形態:進度列與旋轉式載入指示器(Spinner)。本章將聚焦於後者。 Spinne
Android 全域變數與區域變數的使用差異
全域變數可在類別中的多個方法共用,區域變數則只在特定方法或區塊內有效。兩者在 Android 開發中的使用時機不同,混用可能造成非預期的資料狀態。 Adapter 中的