DrawerLayout给我们提供的屏幕滑动拖出选项栏功能,并且布局的过程非常简单。
这个是哔哩哔哩的布局用的就是DrawerLayout,从屏幕左边滑动便可以拖出菜单栏。
文章大纲:
1:什么是DrawerLayout:
2:如何布局Drawerlayout:
3:丰富多彩的Drawerlayout:
环节一: <关于DrawerLayout>
来看看DrawerLayout继承结构:
DrawerLayout是继承的ViewGroup, 我们都知道平时使用的FrameLayout,LinearLayout.....同样都是继承了ViewGroup,所以DrawerLayout的使用也大径相同。而DrawerLayout能滑动无非就是实现了 View的OnTouch或OnTouchEvent方法:
进去源码一看确实,DrawerLayout实现了OnTouchEvent()方法,而FrameLayout等等并没有实现。 方法的具体实现对于我们初学者而言是比较难的,所以知道内部的运行过程即可。
环节二:<如何布局Drawerlayout>
经过了环节一相信大家已经差不多知道如何使用了, 其实就是在需要实现滑动的View上加上父布局DrawerLayout。这样在DrawerLayout能接受到触摸事件的情况下,便会产生相应动作。看xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_green_light">
<TextView
android:layout_width="wrap_content"