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

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

下面看程序截图:

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

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

在SlidingDrawerActivity工程下:

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

[java] view plain copy print ?
  1. <spanstyle="font-size:16px;">packagecom.cn.daming;
  2. importandroid.app.Activity;
  3. importandroid.content.res.Configuration;
  4. importandroid.os.Bundle;
  5. importandroid.widget.GridView;
  6. importandroid.widget.ImageView;
  7. importandroid.widget.SlidingDrawer;
  8. publicclassSlidingDrawerMainActivityextendsActivity{
  9. privateGridViewgridView;
  10. privateSlidingDrawerslidingDrawer;
  11. privateImageViewimageView;
  12. privateint[]icons={
  13. R.drawable.title1,R.drawable.title2,
  14. R.drawable.title3,R.drawable.title4,
  15. R.drawable.title5,R.drawable.title6
  16. };
  17. privateString[]items={
  18. "Phone","Message","AddImage","Music","Telephone","SMS"
  19. };
  20. @Override
  21. publicvoidonCreate(BundlesavedInstanceState){
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. gridView=(GridView)findViewById(R.id.mycontent);
  25. slidingDrawer=(SlidingDrawer)findViewById(R.id.sliding_drawer);
  26. imageView=(ImageView)findViewById(R.id.my_image);
  27. MyGridViewAdapteradapter=newMyGridViewAdapter(this,items,icons);
  28. gridView.setAdapter(adapter);
  29. slidingDrawer.setOnDrawerOpenListener(newSlidingDrawer.OnDrawerOpenListener(){
  30. publicvoidonDrawerOpened(){
  31. imageView.setImageResource(R.drawable.right1);
  32. }
  33. });
  34. slidingDrawer.setOnDrawerCloseListener(newSlidingDrawer.OnDrawerCloseListener(){
  35. publicvoidonDrawerClosed(){
  36. imageView.setImageResource(R.drawable.left1);
  37. }
  38. });
  39. }
  40. @Override
  41. publicvoidonConfigurationChanged(ConfigurationnewConfig){
  42. super.onConfigurationChanged(newConfig);
  43. }
  44. }</span>

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

[java] view plain copy print ?
  1. <spanstyle="font-size:16px;">packagecom.cn.daming;
  2. importandroid.content.Context;
  3. importandroid.view.LayoutInflater;
  4. importandroid.view.View;
  5. importandroid.view.ViewGroup;
  6. importandroid.widget.BaseAdapter;
  7. importandroid.widget.ImageView;
  8. importandroid.widget.TextView;
  9. publicclassMyGridViewAdapterextendsBaseAdapter{
  10. privateContextcontext;
  11. privateString[]items;
  12. privateint[]icons;
  13. publicMyGridViewAdapter(Contextcontext,String[]items,int[]icons){
  14. this.context=context;
  15. this.items=items;
  16. this.icons=icons;
  17. }
  18. publicintgetCount(){
  19. returnitems.length;
  20. }
  21. publicObjectgetItem(intarg0){
  22. returnitems[arg0];
  23. }
  24. publiclonggetItemId(intposition){
  25. returnposition;
  26. }
  27. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  28. LayoutInflaterlayoutInflater=LayoutInflater.from(context);
  29. Viewview=(View)layoutInflater.inflate(R.layout.grid,null);
  30. ImageViewimageView=(ImageView)view.findViewById(R.id.image_view);
  31. TextViewtextview=(TextView)view.findViewById(R.id.text_view);
  32. imageView.setImageResource(icons[position]);
  33. textview.setText(items[position]);
  34. returnview;
  35. }
  36. }
  37. </span>

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

[html] view plain copy print ?
  1. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns: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. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn.daming"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="8"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".SlidingDrawerMainActivity"
  9. android:label="@string/app_name"
  10. android:configChanges="orientation|locale">
  11. <intent-filter>
  12. <actionandroid:name="android.intent.action.MAIN"/>
  13. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  14. </intent-filter>
  15. </activity>
  16. </application>
  17. </manifest></span>


补充说明:

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

看下截图效果:

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

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

[html] view plain copy print ?
  1. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns: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. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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>


原文地址: http://blog.csdn.net/wdaming1986/article/details/6898374

更多相关文章

  1. android 控制EditText字符长度[配置控制 代码控制]
  2. android中控制AlertDialog的关闭
  3. 对android重力测试的一个疑问
  4. Android--Toast 两个 Crash
  5. Kotlin + Mvp + RxJava + Retrofit 心得体会
  6. Android中防止重复点击的小技巧
  7. Android中activity跳转与Intent传值(重复)
  8. Android(安卓)好的源码依赖包 收集
  9. [已解决]eclipse+ADT+Android(安卓)SDK 搭建Android(安卓)开发环境

随机推荐

  1. tab切换
  2. Vue:1.Vue组件的注册与挂载流程,实例演示;
  3. 1. 为翻页按钮添加功能; 2. 当鼠标移出时
  4. QLC SSD在未来10年都不可能取代HDD?希捷对
  5. Nvidia关机后自动释放授权
  6. 0416作业-Vue组件及路由
  7. 三维模型修饰、单体化,水面修复,道路还原,悬
  8. IDC公布全球4Q20数据中心三大件市场数据,
  9. 物流快递单号智能识别接口API案例代码
  10. 4QCY20各大企业存储公司财报汇总