布局简介
SlidingDrawer(滑动式抽屉)隐藏屏外的内容,并允许用户拖拽一个handle以显示隐藏的内容。SlidingDrawer可以在垂直或者水平使用。它由两个子视图组成:一个是用户拖拽的handle(柄),另一个是随着拖动变化的content(内容)在XML布局中SlidingDrawer必须指定handle和content的id。
效果图
布局详解
一个抽屉有抽屉把和抽屉盒组成,本布局也是一样
- <SlidingDrawer
- android:id="@+id/drawer1"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:content="@+id/Viewcontent"
- android:handle="@+id/handle"
- android:orientation="horizontal"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
- </SlidingDrawer>
上面的代码是右侧抽屉的代码,我们抽屉把手和抽屉内容都还没写,先来详解一下代码
android:content=”@+id/Viewcontent”,是抽屉的布局内容的ID
android:handle=”@+id/handle”,为抽屉把手的布局ID
android:orientation=”horizontal”,SlidingDrawer的方向。可选horizontal(水平方向对齐)和vertical( 竖直方向对齐)
可选设置代码
android:allowSingleTap,指示是否可通过单击handle打开或关闭(如果是false,刚用户必须通过拖动,滑动或者使用轨迹球,来打开/关闭抽屉。)默认的是true。
android:animateOnClick,指示当用户点击handle的时候,抽屉是否以动画的形式打开或关闭。默认的是true。
android:bottomOffset,Handle距离SlidingDrawer底部的额外距离
接着是布局把手和抽屉内容,下面是完整代码(已经包含上面代码)
- <SlidingDrawer
- android:id="@+id/drawer1"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:content="@+id/Viewcontent"
- android:handle="@+id/handle"
- android:orientation="horizontal"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
- <!--设置引导按钮-->
- <LinearLayout
- android:id="@id/handle"
- android:layout_width="35dp"
- android:layout_height="match_parent"
- android:gravity="center">
- <ImageView
- android:id="@+id/myImage1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/handle_img" />
- </LinearLayout>
- <!--设置抽屉内容-->
- <ImageView
- android:id="@+id/Viewcontent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/iv_bg" />
- </SlidingDrawer>
设置好后,运行即可显示图中效果图了
完善抽屉
你可以发现一个问题,那就是只能点击把手和拖动把手才能让抽屉还原,如果点击抽屉外面部分,抽屉并不会还原,所以需要我们重写SlidingDrawer的触摸事件。
Java代码如下
- slidingDrawer.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (!slidingDrawer.isMoving()) {
- if (slidingDrawer.isOpened()) {
- slidingDrawer.close();
- }
- }
- return false;
- }
- });
其中slidingDrawer为通过ID寻找到的SlidingDrawer名称,自行绑定即可。
SlidingDrawer为我们提供了两个判断函数,isMoving()是指判断是否正在移动,我们不能在抽屉拖动的时候对它进行关闭,(现在手机都是多点触摸,如果不判断的话,就会出问题)
isOpened()指是否已经开启抽屉,如果已经打开我们才关闭,没有打开无需执行事件
写在最后
Android 6已经不建议使用了,但是用的还是挺方便的,虽说是向下兼容的,但是对于更多的功能需求,SlidingDrawer是无法满足的了
源码下载:http://download.youkuaiyun.com/download/applek_case/10253297
本文介绍了Android中的SlidingDrawer控件,详细解析了其XML布局,包括handle和content的设置,以及horizontal方向的配置。示例代码展示了如何创建一个包含handle和content的抽屉,并提供了调整抽屉行为的可选设置。文章还提到,虽然在Android 6后不再推荐使用SlidingDrawer,但在向下兼容方面仍有一定实用性。
218

被折叠的 条评论
为什么被折叠?



