implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.android.support:design:26.1.0'
  • xml布局文件导入menu的头部和功能选项(注销)
    //activity对应的xml文件
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />
    //新建一个menu目录,建nav_menu.xml文件
    右键res-->Android Resource Direction-->在Resource type下拉框选menu...-->end
    <?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/nav_logout"
            android:title="注销" />
    </group>
    </menu>
    //layout中新建nav_header.xml文件,ic_done图片顺便找一张
    <?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="180dp"
    android:background="?attr/colorPrimary"
    android:padding="10dp">
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/icon_image"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_done" />
    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:textColor="#FFF"
        android:textSize="14sp" />
    </RelativeLayout>

    效果:

activity实现功能

private NavigationView navigationView;
//从application中获取用户信息(在head中显示用户名--忽略)
private MyApplication myApplication = MyApplication.getInstance();
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        。。。。。。
        //菜单栏设置点击事件
        menu();
    }
private void menu(){
        navigationView = findViewById(R.id.nav_view);
        //获取头布局文件
        View headerView = navigationView.getHeaderView(0);
        TextView textview = (TextView)headerView.findViewById(R.id.username);
        textview.setText(myApplication.getLoginUser().getUsername());
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
//注销功能--忽略
                switch (item.getItemId()){
                    case R.id.nav_logout:
                        myApplication.userLogout();
                        Intent intent=new Intent(IndexActivity.this,MainActivity.class);
                  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        break;
                }
                return false;
            }
        });
    }

ref:使用NaviGationView来配置SlidingMenu的左侧菜单——学习笔记

安卓和之前的j2ee大作业差不多就完结--这几天要做个总结||||||||||