編輯:Android 開發實例
最近在開發一個應用時,我希望把設定頁面和主頁面放在一起,讓使用者能即時預覽設定對 UI 的影響。SlidingDrawer 這個控制項正好能實現這種「抽屜」效果——拉開時佔據半個螢幕,另一半仍顯示主頁面,體驗相當不錯。
其實 Android 的 Launcher 就是一個抽屜,點開它就能看到已安裝的應用。下面我們就來做一個類似的效果。SlidingDrawer 的使用非常簡單,幾乎全靠 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>
在 SlidingDrawer 標籤下,android:handle 指向抽屜的手把圖片,android:content 則指向抽屜展開後顯示的內容。有了這兩者,一個抽屜就成形了。
接著看 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);// 自定義 MyAdapter 實現圖標加 item 的顯示
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 來顯示「圖標 + 文字」的格狀排列:
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]; }\br /> @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 = (View) factory.inflate(R.layout.gv, null);// 綁定自定義 layout
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>


如此一來,抽屜就完成了。這個控制項非常實用,除了設定面板,還能應用於許多情境,發揮你的想像力,抽屜將為你的應用增色不少。
自製 TabMenu 彈出選單:封裝成可重複使用的複合控件
用過 UC 瀏覽器 Android 版的人,大概都對它那個分頁式選單有印象。Android 原生的 OptionsMenu 有個先天限制:超過六項就被折進「更多」裡,能呈
Android EditView 控件常用屬性與監聽技巧全解析
什麼是 EditView 在 Android 的界面體系裡,EditView(正式名稱為 EditText)扮演著「可輸入文本框」的角色。它並不是一個全新的控件,而是繼承
Android 進階:藍牙傳感應用實例
前文介紹過 Android 利用麥克風采集並顯示模擬信號的方法,這種方式適用於沒有 IO 控制、單純讀取信號的場景。但如果傳感器本身還需要控制電路(例如採集血氧信號時要紅
Android 開發技巧:用 ViewStub 實現控制項的惰性載入
前面介紹過 <include> 標籤,它可以在佈局檔中引用另一個佈局檔,並覆蓋被引用佈局根節點所有以 android:layout 開頭的相關屬性。透過 &l