SlidingDrawer抽屜類顯示

SlidingDrawer 是自SDK 1.5 才新加入的成員,也許你已曾經在Android 手機上看過。按下
一個按鈕,就能展開一個"程序集"菜單,裏面包含了各式各樣的程序,而SlidingDrawer
Widget 正是爲了這樣的效果所準備的,當你在佈局有限的UI Layout 時,可以應用
SlidingDrawaer 來在可視範圍內放置更多組件,在需要的時候才拉出"抽屜"裏的"子功能圖標
"。SlidingDrawer 配置上採用了水平展開或垂直展開兩種(android:orientation)方式,在XML
裏必須指定其使用的android:handle 與android:content,前者委託要展開的圖片(Layout 配
置),後者則是要展開的Layout Content。

運行結果如下

   SlidingDrawer 的使用,並不需複雜的設置才能啓用,關鍵在於XML 裏的屬性設置,稍後
將會看到XML 裏的描述。在主程序中所建立的SlidingDrawer 對象,爲了捕捉"打開完畢"與"
已經關閉"這兩個事件,所設置的Listener 爲SlidingDrawer.setOnDrawerOpenListener()與
SlidingDrawer.setOnDrawerCloseListener()。

 

 1.  

 2.   import android.widget.GridView; 

 3.   import android.widget.ImageView;

 4.    import android.widget.SlidingDrawer;

   5.

   6.   public class EX04_27 extends Activity

   7.    {

   8.     private GridView gv;

   9.     private SlidingDrawer sd;

   10.    private ImageView im;

   11.    private int[] icons={R.drawable.alarm,R.drawable.calendar,

   12.                         R.drawable.camera,R.drawable.clock,

   13.                         R.drawable.music,R.drawable.tv};

   14.    private String[] items=

   15.     {

   16.       "Alarm","Calendar","Camera","Clock","Music","TV"

   17.     };

   18.

   19.    

   20.     @Override

   21.    public void onCreate(Bundle savedInstanceState)

   22.     {

   23.       super.onCreate(savedInstanceState);

   24.      

   25.       setContentView(R.layout.main);

   26.      

   27.       gv = (GridView)findViewById(R.id.myContent1);

   28.       sd = (SlidingDrawer)findViewById(R.id.drawer1);

   29.       im=(ImageView)findViewById(R.id.myImage1);

   30.

   31.      

   32.      MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons);

   33.       gv.setAdapter(adapter);

   34.

   35.      

   36.       sd.setOnDrawerOpenListener

   37.       (new SlidingDrawer.OnDrawerOpenListener()

       

 38.       {

    39.         @Override

    40.         public void onDrawerOpened()

    41.          {

    42.            im.setImageResource(R.drawable.close);

    43.          }

    44.       });

    45.      

    46.       sd.setOnDrawerCloseListener

    47.        (new SlidingDrawer.OnDrawerCloseListener()

    48.       {

    49.         @Override

    50.         public void onDrawerClosed()

    51.          {

    52.            im.setImageResource(R.drawable.open);

    53.          }

    54.       });

    55.     }

    56.   }

    MyGridViewAdapter 在本範例中,是爲了"拉開SlindingDrawer"所要顯示的GridView 配置
的圖標,以下是自定義繼承自BaseAdapter 的類。

    1.   

    2.

    3.   

    4.    public class MyGridViewAdapter extends BaseAdapter

    5.    {

    6.      private Context _con;

    7.      private String[] _items;

    8.      private int[] _icons;

    9.     

    10.     public MyGridViewAdapter(Context con,String[] items,int[] icons)

    11.     {

    12.      _con=con;

    13.      _items=items;

    14.      _icons=icons;

    15.     }

     16.

     17.     @Override

     18.     public int getCount()

     19.     {

     20.       return _items.length;

     21.     }

     22.

     23.     @Override

     24.     public Object getItem(int arg0)

     25.     {

     26.       return _items[arg0];

     27.     }

     28.

     29.     @Override

     30.     public long getItemId(int position)

     31.     {

     32.       return position;

     33.     }

     34.

     35.     @Override

     36.     public View getView(int position, View convertView, ViewGroup parent)

     37.     {

     38.       LayoutInflater factory = LayoutInflater.from(_con);

     39.      

     40.       View v = (View) factory.inflate(R.layout.grid, null);

     41.      

     42.       ImageView iv = (ImageView) v.findViewById(R.id.icon);

     43.       TextView tv = (TextView) v.findViewById(R.id.text);

     44.      

     45.       iv.setImageResource(_icons[position]);

     46.       tv.setText(_items[position]);

     47.       return v;

     48.     }

     49.   }

res/layout/main.java   

 

     這支XML 中的SlidingDrawer TAG,通過Activity 裏onCreate() 方法中的setContent-
View(R.layout.main)之後,就會存在於佈局(Layout )當中,即使主程序(EX04_27.java)沒有
編寫相關的程序,依然可以正常運行,關鍵在於當中android:handle 指定要顯示的ImageView
(小圓圖)作爲開關,android:content 則是按下這個ImageView (開關)之後,所要展開抽屜
顯示的佈局(Layout)。

   1.   <?xml version="1.0" encoding="utf-8"?>

   2.   <RelativeLayout

   3.      xmlns:android="http://schemas.android.com/apk/res/android"

   4.      android:layout_width="fill_parent"

   5.      android:layout_height="fill_parent"

   6.    >

   7.     <TextView

   8.        android:layout_width="fill_parent"

   9.        android:layout_height="wrap_content"

   10.       android:text="@string/hello"

   11.       android:textSize="16sp"

   12.     />

   13.    <SlidingDrawer

   14.       android:id="@+id/drawer1"

   15.       android:layout_width="fill_parent"

   16.       android:layout_height="fill_parent"

   17.       android:handle="@+id/layout1"

   18.       android:content="@+id/myContent1"

   19.       android:orientation="horizontal"

   20.     >

   21.      <LinearLayout

   22.         android:id="@id/layout1"

   23.         android:layout_width="35px"

   24.         android:layout_height="fill_parent"

   25.         android:background="@drawable/black"

   26.         android:gravity="center"

   27.       >

   28.        <ImageView

   29.           android:id="@+id/myImage1"

   30.           android:layout_width="wrap_content"

   31.          android:layout_height="wrap_content"

   32.          android:src="@drawable/open"

   33.        />

   34.      </LinearLayout>

   35.      <GridView

   36.        android:id="@id/myContent1"

   37.        android:layout_width="wrap_content"

   38.        android:layout_height="wrap_content"

   39.        android:numColumns="2"

   40.        android:background="@drawable/black"

   41.        android:gravity="center"

   42.      />

   43.    </SlidingDrawer>

   44.  </RelativeLayout>

 

上面的XML 程序代碼之中是配置以水平方式來打開抽屜,我們另外通過修改
android:orientation="vertical",就能讓SlidingDrawer 以垂直的方式打開。

   1.   <SlidingDrawer

   2.     android:id="@+id/drawer1"

   3.     android:layout_width="fill_parent"

   4.     android:layout_height="fill_parent"

   5.     android:handle="@+id/layout1"

   6.     android:content="@+id/myContent1"

   7.     android:orientation="vertical"

   8.   >

android中SlidingDrawer(抽屜)的應用android中SlidingDrawer(抽屜)的應用


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章