滑动菜单

DrawerLayout(抽屉布局)

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </FrameLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:text="Hello,world"/>
</androidx.drawerlayout.widget.DrawerLayout>

这里的layout_gravity用于指示隐藏的菜单在从左还是从右,这里指定start表示是根据系统语言来定的。

添加滑动菜单的导航按钮

如果只是按照上面的方法的话,用户根本不知道还有滑动菜单这个东西,所以我们需要添加一个导航功能来提示用户,在Toolbar中留了一个位置给这个功能(HomeAsUp按钮)。
首先将图标加到Toolbar的这个位置。

      drawerLayout=findViewById(R.id.drawer_layout);
        if(actionBar!=null){
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

接着,在onOptionItemSelected()中进行处理。

	//HomeAsUp按钮的id永远是android.R.id.home
     case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;

NevigationView进行优化

可以发现的是之前做的滑动菜单还是不够精美。NevigationView是Design Support库中提供的一个控件,是按照Material Design的要求来进行设计的,而且将滑动菜单的页面的实现变的非常的简单。
思路是,在NevigationView控件中添加nav_header.xml和nav_menu.xml。

nav_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="?attr/colorPrimary"
    android:layout_height="match_parent">
    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true"
        android:src="@drawable/cute_girl"/>
</RelativeLayout>

nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/call"
        android:icon="@drawable/nav_call"
        android:title="call"/>
    <item
        android:id="@+id/friends"
        android:icon="@drawable/nav_friends"
        android:title="friends"/>
    <item
        android:id="@+id/location"
        android:icon="@drawable/nav_location"
        android:title="location"/>
    <item
        android:id="@+id/mail"
        android:icon="@drawable/nav_mail"
        android:title="mail"/>
    <item
        android:id="@+id/task"
        android:icon="@drawable/nav_task"
        android:title="task"/>

</group>
</menu>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/drawer_layout"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </FrameLayout>
    <com.google.android.material.navigation.NavigationView
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu"/>

</androidx.drawerlayout.widget.DrawerLayout>

对menu中的点击事件进行处理。

很简单,findViewById找到控件,然后为控件中的item绑定事件即可。

        drawerLayout=findViewById(R.id.drawer_layout);
        DrawerLayout drawerLayout=findViewById(R.id.drawer_layout);
        NavigationView navigationView =findViewById(R.id.navigation);
        navigationView.setCheckedItem(R.id.call);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                drawerLayout.closeDrawers();
                return true;
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值