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

Android 抽屜效果實作:用 SlidingDrawer 打造隱藏式側邊面板

編輯: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>

如此一來,抽屜就完成了。這個控制項非常實用,除了設定面板,還能應用於許多情境,發揮你的想像力,抽屜將為你的應用增色不少。

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