SlidingDrawer类使用的时候主要包括两点:
1:handle:单击的按钮
2:content:抽屉中的内容,单击按钮时,抽屉的内容隐藏或显示
下面这段xml布局是摘自Google SDK帮助文档:
<SlidingDrawer
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:handle="@+id/handle"
android:content="@+id/content">
<ImageView
android:id="@id/handle"
android:layout_width="88dip"
android:layout_height="44dip" />
<GridView
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</SlidingDrawer>
下面给出一个实例
首先实例的实现截图如下:


实现滑动效果
给出如下实现代码:
1.总体布局文件的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://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="16sp"
/>
<SlidingDrawer
android:id="@+id/drawer1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:handle="@+id/layout1"
android:content="@+id/myContent1"
android:orientation="horizontal"
>
<LinearLayout
android:id="@id/layout1"
android:layout_width="35px"
android:layout_height="fill_parent"
android:background="@drawable/black"
android:gravity="center"
>
<ImageView
android:id="@+id/myImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/open"
/>
</LinearLayout>
<GridView
android:id="@id/myContent1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:background="@drawable/black"
android:gravity="center"
/>
</SlidingDrawer>
</RelativeLayout>
2.GridView中的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://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="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="20sp"
android:gravity="center"
android:textColor="@drawable/white"
/>
</LinearLayout>
3.BaseAdapter的子类实现(为GridView提供数据)
/* 自定义Adapter,继承BaseAdapter */
public class MyGridViewAdapter extends BaseAdapter
{
private Context _con;
private String[] _items;
private int[] _icons;
/* 构造符 */
public MyGridViewAdapter(Context con,String[] items,int[] icons)
{
_con=con;
_items=items;
_icons=icons;
}
@Override
public int getCount()
{
return _items.length;
}
@Override
public Object getItem(int arg0)
{
return _items[arg0];
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater factory = LayoutInflater.from(_con);
/* 使用grid.xml为每几个item的Layout */
View v = (View) factory.inflate(R.layout.grid, null);
/* 取得View */
ImageView iv = (ImageView) v.findViewById(R.id.icon);
TextView tv = (TextView) v.findViewById(R.id.text);
/* 设定显示的Image与文? */
iv.setImageResource(_icons[position]);
tv.setText(_items[position]);
return v;
}
}
4.主程序的实现
public class EX04_27 extends Activity
{
private GridView gv;
private SlidingDrawer sd;
private ImageView im;
private int[] icons={R.drawable.alarm,R.drawable.calendar,
R.drawable.camera,R.drawable.clock,
R.drawable.music,R.drawable.tv};
private String[] items={"Alarm","Calendar","Camera","Clock","Music","TV"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.main);
/* 初始化对象 */
gv = (GridView)findViewById(R.id.myContent1);
sd = (SlidingDrawer)findViewById(R.id.drawer1);
im=(ImageView)findViewById(R.id.myImage1);
/* 使用告定义的MyGridViewAdapter设置GridView里面的item内容 */
MyGridViewAdapter adapter=new MyGridViewAdapter(this,items,icons);
gv.setAdapter(adapter);
/* 设定SlidingDrawer被打开的事件处理 */
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()
{
@Override
public void onDrawerOpened()
{
im.setImageResource(R.drawable.close);
}
});
/* 设置SlidingDrawer被关闭的事件处理 */
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener()
{
@Override
public void onDrawerClosed()
{
im.setImageResource(R.drawable.open);
}
});
}
}
分享到:
相关推荐
标题与描述中提到的知识点是关于Android开发中的`SlidingDrawer`组件的应用。在Android开发中,`SlidingDrawer`是一种用于创建滑动抽屉效果的布局容器,它允许用户通过触摸屏幕边缘来展开或收起一个包含额外控件的...
在早期的Android版本中,SlidingDrawer是开发者常用来创建侧边栏菜单的工具,但在Android设计指南推荐使用NavigationView之后,其使用频率有所下降。尽管如此,SlidingDrawer仍然在某些特定场景下有其独特的价值,...
Android Launcher抽屉类SlidingDrawer的使用 AndroidLauncher抽屉类SlidingDrawer是一个非常有用的控件,它可以实现抽屉式的交互效果,用户可以通过点击handle来打开或关闭抽屉,里面可以放置各种控件,例如Button...
在Android开发中,`SlidingDrawer`是一个非常实用的组件,它允许用户通过手势从屏幕边缘滑动出一个隐藏的抽屉,常用于存放菜单、设置或其他附加功能。本范例程序深入展示了如何有效地实现这一功能,使得用户界面更加...
6. **兼容性问题**:由于SlidingDrawer在新版本的Android中已不推荐使用,因此在不同版本的Android设备上可能会存在兼容性问题。如果需要在现代Android应用中实现类似功能,可以考虑使用如前面提到的...
尽管SlidingDrawer在早期的Android版本中被广泛使用,但它在API 17及以后的版本中已被弃用。因此,对于新项目,开发者通常会转向使用其他替代组件,如`android.support.design.widget.BottomSheetBehavior`(自...
这个实例是专为初学者设计的,展示了如何使用SlidingDrawer从底部向上滑动的效果。 首先,我们需要了解SlidingDrawer的基本用法。在布局XML文件中,SlidingDrawer作为顶级容器添加,有两个必需的子元素:一个用于...
SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of ...
在本篇Android学习笔记中,我们将探讨如何使用SlidingDrawer实现多页显示,并理解其工作原理和相关属性。 首先,SlidingDrawer通常用于在主界面下方或上方滑出额外的内容,例如菜单、设置或附加信息。它有两个主要...
1. **在XML布局文件中添加SlidingDrawer**:使用`<SlidingDrawer>`标签,并设置`android:handle`为手柄视图的ID,`android:content`为内容视图的ID。 2. **设置属性**:可以设置抽屉的开启状态(`android:open`)、...
综上所述,虽然 SlidingDrawer 在现代 Android 开发中已较少使用,但理解它的工作原理对于理解滑动抽屉的设计模式和如何在新组件中实现类似效果仍然是有价值的。如果你的项目还在使用 SlidingDrawer,可能需要考虑...
Aj_01 来自:http://blog.csdn.net/Android_Tutor/archive/2010/04/14/5486804.aspx 测试:Android Launcher抽屉类SlidingDrawer的使用!
在Android开发中,`SlidingDrawer`是一个非常实用的组件,它允许用户通过手势从屏幕边缘滑出一个隐藏的视图,类似于许多应用中的抽屉式导航菜单。在这个实例中,我们将关注如何实现一个从左侧拉出的`SlidingDrawer`...
Android高手进阶教程之----Android Launcher抽屉类SlidingDrawer的使用.doc Android高手进阶教程之----Android Location的使用!! .doc Android高手进阶教程之----Android PopupWindow的使用!!! .doc Android高手...
总之,虽然SlidingDrawer已不再推荐使用,但了解它的基本原理和使用方法可以帮助我们更好地理解和实现Android中的滑动抽屉效果。在实际项目中,开发者应优先考虑使用更新的组件来保证应用的兼容性和最佳用户体验。
用SlidingDrawer注意两点,一个是android:handle(委托要展开的图片加载Layout配置) 和android:content(要展开的Layout Content), 我的csdn博客地址: ...
然而,随着Android设计指南的更新,Google推荐使用`NavigationView`或`DrawerLayout`来构建侧滑菜单,因为SlidingDrawer已被弃用,不适用于新版本的Android系统。 这个项目包含多种抽屉实例,包括上拉、下拉、左拉...
在Android开发中,SlidingDrawer 是一个非常常见的组件,它允许用户通过拖动来显示或隐藏内容,类似于抽屉的效果。然而,标准的SlidingDrawer组件在某些情况下可能无法满足开发者的需求,比如需要部分数据始终可见。...
在Android应用开发中,抽屉(SlidingDrawer)是一个非常实用的组件,它允许开发者在屏幕上添加一个可滑动的面板,通常用于隐藏或显示额外的功能或内容。抽屉类在早期版本的Android SDK中被广泛使用,但在后来的版本...