android 抽屉式布局的使用


1.首先定义主布局
 

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.dejun.androidcourse.activity.ThirdActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipChildren="false">

        <com.dejun.commonsdk.wight.CustomSearchView
            android:id="@+id/csv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:search_backgound="#ff00"
            app:search_hint="搜索内容"
            app:search_left_icon="@drawable/arrow_left_back"
            app:search_right_text="完成"></com.dejun.commonsdk.wight.CustomSearchView>
        <com.dejun.commonsdk.wight.BottomMenuView
            android:id="@+id/bmv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:fifth_drawable="@drawable/tab_better_pressed"
            app:fifth_text="你好"
            app:menu_center_visible="true"
            app:menu_text_color="#000"
            app:menu_text_size="12sp" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/csv"
            android:layout_above="@id/bmv">
        </FrameLayout>
    </RelativeLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigationview"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"//定义抽屉式布局的左侧布局
        app:headerLayout="@layout/drawer_header_layout"/左侧放置头像的布局
        app:menu="@menu/drawer_menu"//左侧放置菜单的布局
        app:itemIconTint="#ff00"//定义菜单图片颜色
        />
</android.support.v4.widget.DrawerLayout>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/left_head_background"
        android:scaleType="centerCrop"/>

    <ImageView
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:padding="8dp"
        android:src="@drawable/default_avatar"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:padding="16dp"
        android:textColor="#000"
        android:textSize="17sp"
        android:text="魏群" />

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_one"
            android:icon="@drawable/drawer_default_icon"
            android:title="文件" />
        <item
            android:id="@+id/navigation_item_two"
            android:icon="@drawable/drawer_default_icon"
            android:title="目录" />
        <item
            android:id="@+id/navigation_item_three"
            android:icon="@drawable/drawer_default_icon"
            android:title="佳佳" />
        <item
            android:id="@+id/navigation_item_four"
            android:icon="@drawable/drawer_default_icon"
            android:title="闹钟" />
        <item
            android:id="@+id/navigation_item_five"
            android:icon="@drawable/drawer_default_icon"
            android:title="设置" />


        <!--<item-->
            <!--android:id="@+id/navigation_subheader_four"-->
            <!--android:icon="@drawable/drawer_default_icon"-->
            <!--android:title="子条目">-->
            <!--<menu>-->
                <!--<item-->
                    <!--android:id="@+id/navigation_sub_item_eight"-->
                    <!--android:icon="@drawable/drawer_default_icon"-->
                    <!--android:title="佳佳" />-->
                <!--<item-->
                    <!--android:id="@+id/navigation_sub_item_nine"-->
                    <!--android:icon="@drawable/drawer_default_icon"-->
                    <!--android:title="闹钟" />-->
            <!--</menu>-->
        <!--</item>-->
    </group>
</menu>
3.使用

drawerLayout=findViewById(R.id.drawerlayout);
navigationview=findViewById(R.id.navigationview);
headerView = (FrameLayout) navigationview.getHeaderView(0);
navigationview.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_item_one:
                break;
            case R.id.navigation_item_two:
                break;
            case R.id.navigation_item_three:
                break;
            case R.id.navigation_item_four:
                break;
            case R.id.navigation_item_five:
                break;
        }
        return false;
    }
});
4.关闭侧滑菜单

drawerLayout.openDrawer(Gravity.LEFT);
 
 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值