借助DrawerLayout实现侧滑菜单

来源:互联网 发布:淘宝超级品牌日 编辑:程序博客网 时间:2024/05/18 15:05

使用的时候根布局是

android.support.v4.widget.DrawerLayout

在AndroidStudio中好像是直接可以使用,据说eclipse要先自己添加.
在drawerLayout中设置多个子控件.第一个子控件默认是主界面,不需要侧滑菜单是显示的部分写在这里.之后放的控件需要有属性

android:layout_gravity

选start或者end表示菜单在左侧或者右侧.
如果先东西太多可以分别在别的xml中写界面,写好在include进去.

如果需要通过按钮唤出或关闭菜单需要用到drawerLayou.openDrawer(drawerView)/closeDrawer(drawerView)方法,drawerView是在DrawerLayout布局中的第二个子节点(菜单布局).

activity_main

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/dl_left"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:fitsSystemWindows="true"    android:clipToPadding="true">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <include layout="@layout/activity_zhu"/>    </FrameLayout>    <RelativeLayout        android:layout_width="240dp"        android:layout_height="match_parent"        android:background="#ad85d2f7"        android:layout_gravity="start"        >        <include layout="@layout/activity_menu"/>    </RelativeLayout></android.support.v4.widget.DrawerLayout>

activity_zhu

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="top"        android:orientation="vertical"        tools:context=".activity.ActivityMain">        <include layout="@layout/title_with_text"/>        <FrameLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            >            <FrameLayout                android:id="@+id/fragment"                android:layout_width="match_parent"                android:layout_height="match_parent"                >            </FrameLayout>            <ImageButton                android:id="@+id/fab_add"                android:layout_width="56dp"                android:layout_height="56dp"                android:layout_gravity="bottom|right"                android:layout_margin="20dp"                android:background="@drawable/fab_background"                android:src="@drawable/fab_src_add"                />        </FrameLayout>        <LinearLayout            android:id="@+id/m"            android:layout_width="match_parent"            android:layout_height="45dp"            android:background="#F7F9FA">            <RelativeLayout                android:id="@+id/table_zichan"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:clickable="true">                <ImageView                    android:id="@+id/table_img_zichan"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentTop="true"                    android:layout_centerHorizontal="true"                    android:src="@drawable/table_unfocus_zichan"/>                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_alignParentBottom="true"                    android:layout_centerHorizontal="true"                    android:textColor="@color/colorTableText"                    android:text="资产"/>            </RelativeLayout>            <RelativeLayout                android:id="@+id/table_huikuan"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:clickable="true">                <ImageView                    android:id="@+id/table_img_huikuan"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentTop="true"                    android:layout_centerHorizontal="true"                    android:src="@drawable/table_unfocus_huikuan"/>                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_alignParentBottom="true"                    android:layout_centerHorizontal="true"                    android:textColor="@color/colorTableText"                    android:text="回款"/>            </RelativeLayout>            <RelativeLayout                android:id="@+id/table_shijianzhou"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:clickable="true">                <ImageView                    android:id="@+id/table_img_shijianzhou"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentTop="true"                    android:layout_centerHorizontal="true"                    android:src="@drawable/table_unfocus_shijianzhou"/>                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_alignParentBottom="true"                    android:layout_centerHorizontal="true"                    android:textColor="@color/colorTableText"                    android:text="时间轴"/>            </RelativeLayout>            <RelativeLayout                android:id="@+id/table_faxian"                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:clickable="true">                <ImageView                    android:id="@+id/table_img_faxian"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentTop="true"                    android:layout_centerHorizontal="true"                    android:src="@drawable/table_unfocus_faxian"/>                <TextView                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_alignParentBottom="true"                    android:layout_centerHorizontal="true"                    android:textColor="@color/colorTableText"                    android:text="发现"/>            </RelativeLayout>        </LinearLayout>    </LinearLayout></LinearLayout>

activity_menu

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ListView        android:id="@+id/menu_listview"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </ListView></LinearLayout>

不过据说Android7.x就要取消侧滑菜单…这还想Android吗(摔

还是很好用的….

0 0
原创粉丝点击