Android : 侧滑菜单

需要导入的jar包 com.android.support:design:28.0.0

建立一个menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!--orderInCategory 设置优先级-->
    <item android:id="@+id/Item_one"
         android:title="吃饭"
        android:orderInCategory="30"/>
    <item android:id="@+id/Item_Two"
        android:title="睡觉"
        android:orderInCategory="20"/>
    <item android:id="@+id/Item_Three"
        android:title="打豆豆"
        android:orderInCategory="10"/>
    <!--设置一个数组-->
    <group android:id="@+id/group1">
        <item android:id="@+id/Item_four"
            android:title="设置"/>


        <item android:id="@+id/five"
            android:title="我的"/>

    </group>



</menu>

xml里面的代码

<?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/mDrawer"
    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:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="heheda" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我是内容页" />
    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/NaviView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/layout_header"
        app:menu="@menu/menu_heard"></android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

侧滑菜单的头部布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/head_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher_round" />
</RelativeLayout>

Activity里面的代码
package com.example.mydraw;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private NavigationView NaviView;
private ImageView img;
private DrawerLayout mDrawer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    mDrawer = (DrawerLayout) findViewById(R.id.mDrawer);
    NaviView = (NavigationView) findViewById(R.id.NaviView);
    //从navi里面获取head的布局
    View view = NaviView.getHeaderView(0);
    img = view.findViewById(R.id.head_img);

    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "睡个屁,起来嗨", Toast.LENGTH_LONG).show();
        }
    });
    //菜单的点击事件
    NaviView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            //进行判断
            switch (menuItem.getItemId()) {
                case R.id.Item_one:
                    Toast.makeText(MainActivity.this, "睡个屁,起来嗨1", Toast.LENGTH_LONG).show();
                    mDrawer.closeDrawers();
                    break;
                case R.id.Item_Two:
                    Toast.makeText(MainActivity.this, "睡个屁,起来嗨2", Toast.LENGTH_LONG).show();
                    mDrawer.closeDrawers();
                    break;
                case R.id.Item_Three:
                    Toast.makeText(MainActivity.this, "睡个屁,起来嗨3", Toast.LENGTH_LONG).show();
                    mDrawer.closeDrawers();
                    break;
            }


            return false;
        }
    });


}

写侧滑菜单的时候需要注意,如果写了两个主布局需要删除其中一个,否则就会报IllegalStateException(非法语句异常),将其中的一个主布局删掉就好;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值