参考网址
一、删除xml文件中的静态引用
找到<NavigationView>
控件,将控件里的静态引用删除
1 2 3 4 5 6 7 8
| <android.support.design.widget.NavigationView app:theme="@style/myNavigationDrawerStyle" android:id="@+id/nav_view" android:layout_width="280dp" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_menu"/>
|
1 2
| - app:headerLayout="@layout/nav_header" - app:menu="@menu/nav_menu"
|
二、绑定控件
在MainActivity的onCreate()
函数中绑定控件<NavigationView>
1
| + NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
|
1 2 3 4 5
| NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
//引入header和menu + navView.inflateHeaderView(R.layout.nav_header); + navView.inflateMenu(R.menu.nav_menu);
|
四、设置监听事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
//引入header和menu navView.inflateHeaderView(R.layout.nav_header); navView.inflateMenu(R.menu.nav_menu);
+ View navHeaderView =navView.getHeaderView(0); + CircleImageView circleimageview = (CircleImageView)navHeaderView.findViewById(R.id.icon_image); + circleimageview.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View view){ + //用户头像点击事件 + Toast.makeText(MainActivity.this,"你点击了用户头像",Toast.LENGTH_SHORT).show(); + } + });
|
五、效果