DrawLayout的使用

来源:互联网 发布:软件做账 编辑:程序博客网 时间:2024/06/04 18:46

DrawLayout和SlidingMenu有点相识,但使用相当的简单,这是一个在v4包下的控件

android.support.v4.widget.DrawerLayout,这个为控件的全名


首先,先来写下布局,很简单的


<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/dl"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"          >   <!-- 内容区域 -->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical" >        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="内容"            android:textColor="#ffffff" />    </LinearLayout>        <!-- 菜单区域,注意。这个菜单区域的布局要是写下主内容区域前面,将不能用手指划回去,只能响应单击划回去,想要划回去,必须写在主内容区域的下面 -->     <LinearLayout        android:id="@+id/ll_menu_left"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="left"        android:background="#000000"        android:orientation="vertical"         >        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="菜单"            android:textColor="#ffffff"            />    </LinearLayout></android.support.v4.widget.DrawerLayout>

这样就写好 了,需要注意的是:  菜单区域的布局要是写下主内容区域前面,将不能用手指划回去,只能响应单击划回去,想要划回去,必须写在主内容区域的下面



然后通过findViewbyid,可以对这个控件进行一下关闭和打开的操作,还可以对其进行监听


drawerLayout=(DrawerLayout) findViewById(R.id.dl);//drawerLayout.openDrawer(Gravity.LEFT);//打开左边菜单,//drawerLayout.closeDrawer(Gravity.LEFT);//关闭右边菜单drawerLayout.setDrawerListener(new DrawerListener() {@Overridepublic void onDrawerStateChanged(int arg0) {// TODO Auto-generated method stubLog.i("huang", "onDrawerStateChanged");}@Overridepublic void onDrawerSlide(View arg0, float arg1) {// TODO Auto-generated method stubLog.i("huang", "onDrawerSlide");}@Overridepublic void onDrawerOpened(View arg0) {// TODO Auto-generated method stubLog.i("huang", "打开了");}@Overridepublic void onDrawerClosed(View arg0) {// TODO Auto-generated method stubLog.i("huang", "关闭了");}});


0 0