Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android UI 進階:被忽略的 SlidingDrawer 抽屜元件實戰

編輯: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 由左往右)。

二、Activity:接上監聽器換圖示

抽屜開關時,把手圖示應該要跟著變(例如箭頭朝上/朝下),這靠兩個監聽器完成:

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);    // 關閉時換回「展開」圖
            }
        });
    }
}

三、MyAdapter:圖示加文字下標

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。理解它的原理與手勢處理方式,對實作自訂抽屜還是有幫助的。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved