SlidingDrawer

安卓中1.5后加入了SlidingDrawer【隐藏式抽屉】,设计原理在你的UI布局有限的情况下,放不下太多的控件的时候,可以考虑用这个隐藏式抽屉。用SlidingDrawer注意两点,一个是android:handle(委托要展开的图片加载Layout配置) 和android:content(要展开的Layout Content), 转载请标明出处:

 http://blog.csdn.net/wdaming1986/article/details/6898374

 

下面看程序截图:

                 程序开始界面:                                                           点击右边的箭头后出现的界面:

                        SlidingDrawer                                 SlidingDrawer

 

                            点击左边的箭头后出现的界面:

                        SlidingDrawer

在SlidingDrawerActivity工程下:

一、在com.cn.daming包下的SlidingDrawerMainActivity.java类中的代码:

[java]  view plain copy print ?
  1. <span style="font-size:16px;">package com.cn.daming;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.res.Configuration;  
  5. import android.os.Bundle;  
  6. import android.widget.GridView;  
  7. import android.widget.ImageView;  
  8. import android.widget.SlidingDrawer;  
  9.   
  10. public class SlidingDrawerMainActivity extends Activity {  
  11.   
  12.     private GridView gridView;  
  13.     private SlidingDrawer slidingDrawer;  
  14.     private ImageView imageView;  
  15.     private int[] icons={  
  16.         R.drawable.title1, R.drawable.title2,  
  17.         R.drawable.title3, R.drawable.title4,  
  18.         R.drawable.title5, R.drawable.title6  
  19.     };  
  20.       
  21.     private String[] items={  
  22.         "Phone""Message""AddImage""Music""Telephone""SMS"   
  23.     };  
  24.       
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         gridView = (GridView)findViewById(R.id.mycontent);  
  30.         slidingDrawer = (SlidingDrawer)findViewById(R.id.sliding_drawer);  
  31.         imageView = (ImageView)findViewById(R.id.my_image);  
  32.         MyGridViewAdapter adapter = new MyGridViewAdapter(this, items, icons);  
  33.         gridView.setAdapter(adapter);  
  34.         slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {  
  35.               
  36.             public void onDrawerOpened() {  
  37.                 imageView.setImageResource(R.drawable.right1);  
  38.             }  
  39.         });  
  40.         slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {  
  41.               
  42.             public void onDrawerClosed() {  
  43.                 imageView.setImageResource(R.drawable.left1);  
  44.             }  
  45.         });  
  46.     }  
  47.   
  48.     @Override  
  49.     public void onConfigurationChanged(Configuration newConfig) {  
  50.         super.onConfigurationChanged(newConfig);  
  51.     }  
  52. }</span>  

 

二、在com.cn.daming包下的MyGridViewAdapter.java类中的代码:

[java]  view plain copy print ?
  1. <span style="font-size:16px;">package com.cn.daming;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.ImageView;  
  9. import android.widget.TextView;  
  10.   
  11. public class MyGridViewAdapter extends BaseAdapter{  
  12.   
  13.     private Context context;  
  14.     private String[] items;  
  15.     private int[] icons;  
  16.       
  17.     public MyGridViewAdapter(Context context, String[] items, int[] icons){  
  18.         this.context = context;  
  19.         this.items = items;  
  20.         this.icons = icons;  
  21.     }  
  22.       
  23.     public int getCount() {  
  24.         return items.length;  
  25.     }  
  26.   
  27.     public Object getItem(int arg0) {  
  28.         return items[arg0];  
  29.     }  
  30.   
  31.     public long getItemId(int position) {  
  32.         return position;  
  33.     }  
  34.   
  35.     public View getView(int position, View convertView, ViewGroup parent) {  
  36.         LayoutInflater layoutInflater = LayoutInflater.from(context);  
  37.         View view = (View)layoutInflater .inflate(R.layout.grid, null);  
  38.         ImageView imageView = (ImageView)view.findViewById(R.id.image_view);  
  39.         TextView textview = (TextView)view.findViewById(R.id.text_view);  
  40.         imageView.setImageResource(icons[position]);  
  41.         textview.setText(items[position]);  
  42.         return view;  
  43.     }  
  44.   
  45. }  
  46. </span>  

 

三、在res包下的layout下的main.xml中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <TextView  
  7.         android:id="@+id/text_view"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginTop="10dip"  
  11.         android:text="@string/hello"  
  12.         android:textSize="10pt"  
  13.         android:gravity="center"  
  14.     />  
  15.     <SlidingDrawer  
  16.         android:id="@+id/sliding_drawer"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="fill_parent"  
  19.         android:handle="@+id/layout1"  
  20.         android:content="@+id/mycontent"  
  21.         android:orientation="horizontal"  
  22.     >  
  23.         <LinearLayout  
  24.            android:id="@id/layout1"  
  25.            android:layout_width="35px"  
  26.            android:layout_height="fill_parent"  
  27.            android:gravity="center"  
  28.            android:background="#00000000"  
  29.         >  
  30.            <ImageView  
  31.                android:id="@+id/my_image"  
  32.                android:layout_width="wrap_content"  
  33.                android:layout_height="wrap_content"  
  34.                android:src="@drawable/left1"  
  35.            />  
  36.         </LinearLayout>  
  37.         <GridView  
  38.             android:id="@id/mycontent"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:paddingTop="20dip"  
  42.             android:numColumns="3"  
  43.             android:gravity="center"  
  44.             android:background="#ff000000"  
  45.         />  
  46.     </SlidingDrawer>  
  47. </RelativeLayout>  
  48. </span>  

 

四、在res包下的layout下的grid.xml中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     >  
  7.     <ImageView  
  8.         android:id="@+id/image_view"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginBottom="5dip"  
  12.     />  
  13.     <TextView  
  14.         android:id="@+id/text_view"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginBottom="15dip"  
  18.     />  
  19. </LinearLayout>  
  20. </span>  

 

五、在AndroidManifest.xml中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cn.daming"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".SlidingDrawerMainActivity"  
  10.                   android:label="@string/app_name"  
  11.                   android:configChanges="orientation|locale">  
  12.             <intent-filter>  
  13.                 <action android:name="android.intent.action.MAIN" />  
  14.                 <category android:name="android.intent.category.LAUNCHER" />  
  15.             </intent-filter>  
  16.         </activity>  
  17.   
  18.     </application>  
  19. </manifest></span>  


补充说明:

也可以设置垂直的隐藏拉抽屉方式,设置SlidingDrawer中的android:orientation="vertical"。

看下截图效果:

                     点击下拉图标后界面:                                                          点击上拉图标后的界面:

                  SlidingDrawer                     SlidingDrawer 

        

修改下res包下的layout下的main.xml文件中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.    >  
  6.     <TextView  
  7.         android:id="@+id/text_view"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginTop="10dip"  
  11.         android:text="@string/hello"  
  12.         android:textSize="10pt"  
  13.         android:gravity="center"  
  14.     />  
  15.     <SlidingDrawer  
  16.         android:id="@+id/sliding_drawer"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="fill_parent"  
  19.         android:handle="@+id/layout1"  
  20.         android:content="@+id/mycontent"  
  21.         android:orientation="vertical"  
  22.      >  
  23.         <LinearLayout  
  24.            android:id="@id/layout1"  
  25.            android:layout_width="fill_parent"  
  26.            android:layout_height="35px"  
  27.            android:gravity="center"  
  28.            android:background="#00000000"  
  29.         >  
  30.            <ImageView  
  31.                android:id="@+id/my_image"  
  32.                android:layout_width="wrap_content"  
  33.                android:layout_height="wrap_content"  
  34.                android:src="@drawable/up1"  
  35.            />  
  36.         </LinearLayout>  
  37.         <GridView  
  38.             android:id="@id/mycontent"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:paddingTop="20dip"  
  42.             android:numColumns="3"  
  43.             android:gravity="center"  
  44.             android:background="#ff000000"  
  45.         />  
  46.     </SlidingDrawer>  
  47. </RelativeLayout>  
  48. </span>  

 

修改grid.xml文件中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     >  
  7.     <ImageView  
  8.         android:id="@+id/image_view"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginBottom="5dip"  
  12.         android:layout_marginLeft="27dip"  
  13.     />  
  14.     <TextView  
  15.         android:id="@+id/text_view"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_marginBottom="15dip"  
  19.         android:layout_marginLeft="27dip"  
  20.     />  
  21. </LinearLayout>  
  22. </span>  


你可能感兴趣的:(slidingdrawer)