你的位置:首页 > 操作系统

[操作系统]Android Material Design:NavigationView抽屉导航菜单


 

需要添加的包:

 

测试代码:

package com.zzw.navigationview;import android.app.Activity;import android.os.Bundle;import android.support.design.widget.NavigationView;import android.support.design.widget.NavigationView.OnNavigationItemSelectedListener;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;public class MainActivity extends Activity {  private DrawerLayout drawerLayout;  private ImageView imageView;  private NavigationView navigationView;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    initView();    // navigationView选中Item处理    handNavigationView();        findViewById(R.id.button).setOnClickListener(new OnClickListener() {            @Override      public void onClick(View v) {                drawerLayout.openDrawer(GravityCompat.START);       }    });  }  private void handNavigationView() {    navigationView        .setNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {          // 用于辨别此前是否已有选中条目          MenuItem preMenuItem;          @Override          public boolean onNavigationItemSelected(MenuItem menuItem) {            // 首先将选中条目变为选中状态            // 即checked为true,后关闭Drawer,以前选中的Item需要变为未选中状态            if (preMenuItem != null)              preMenuItem.setChecked(false);            menuItem.setChecked(true);            drawerLayout.closeDrawers();            preMenuItem = menuItem;            // 不同item对应不同图片            switch (menuItem.getItemId()) {            case R.id.navigation_item1:              imageView.setImageResource(R.mipmap.bg_one);              break;            case R.id.navigation_item2:              imageView.setImageResource(R.mipmap.bg_two);              break;            case R.id.navigation_item3:              imageView.setImageResource(R.mipmap.bg_three);              break;            case R.id.navigation_sub_item1:              imageView.setImageResource(R.mipmap.bg_four);              break;            case R.id.navigation_sub_item2:              imageView.setImageResource(R.mipmap.bg_five);              break;            case R.id.navigation_sub_item3:              imageView.setImageResource(R.mipmap.bg_default);              break;            }            return true;          }        });  }  private void initView() {    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    imageView = (ImageView) findViewById(R.id.image);    navigationView = (NavigationView) findViewById(R.id.navigation);  }}

 

activity_main.

 

<android.support.v4.widget.DrawerLayout ="http://schemas.android.com/apk/res/android"  ="http://schemas.android.com/apk/res-auto"  android:id="@+id/drawer_layout"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:fitsSystemWindows="true" >  <!-- 主界面 -->  <RelativeLayout    android:id="@+id/parent_layout"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView      android:id="@+id/image"      android:layout_width="match_parent"      android:layout_height="400dp"      android:layout_centerInParent="true"      android:padding="5dp"      android:src="@mipmap/bg_default" />    <Button      android:id="@+id/button"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:layout_centerHorizontal="true"      android:paddingBottom="5dip"      android:text="点击" />    <!-- app:itemIconTint 设置菜单图标颜色  app:itemTextColor设置菜单字体颜色 -->  </RelativeLayout>  <android.support.design.widget.NavigationView    android:id="@+id/navigation"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_gravity="start"    app:headerLayout="@layout/drawer_header"    app:itemIconTint="#2196F3"    app:itemTextColor="#3F51B5"    app:menu="@menu/drawer_menu" >  </android.support.design.widget.NavigationView></android.support.v4.widget.DrawerLayout>

 

 

drawer_header.

<??><RelativeLayout ="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="180dp"  android:background="@mipmap/bg_header"  android:paddingBottom="15dp"  android:paddingLeft="20dp" >  <ImageView    android:id="@+id/photo"    android:layout_width="80dp"    android:layout_height="80dp"    android:layout_above="@+id/desc"    android:layout_marginBottom="20dp"    android:src="@mipmap/photo" />  <TextView    android:id="@+id/desc"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignParentBottom="true"    android:layout_marginBottom="2dp"    android:text="www.cnblogs.com/zzw1994"    android:textColor="#ffffff"    android:textSize="20sp"    android:textStyle="bold" /></RelativeLayout>

 

drawer_menu.

<??><menu ="http://schemas.android.com/apk/res/android" >  <group>    <item      android:id="@+id/navigation_item1"      android:checkable="true"      android:icon="@mipmap/icon_hot"      android:title="热门图片"/>    <item      android:id="@+id/navigation_item2"      android:checkable="true"      android:icon="@mipmap/icon_people"      android:title="联系人"/>    <item      android:id="@+id/navigation_item3"      android:checkable="true"      android:icon="@mipmap/icon_photos"      android:title="图库"/>  </group>  <item    android:id="@+id/navigation_sub"    android:title="新增功能">    <menu>      <item        android:id="@+id/navigation_sub_item1"        android:checkable="true"        android:icon="@mipmap/icon_local"        android:title="地图导航"/>      <item        android:id="@+id/navigation_sub_item2"        android:checkable="true"        android:icon="@mipmap/icon_event"        android:title="最近热门"/>      <item        android:id="@+id/navigation_sub_item3"        android:checkable="true"        android:icon="@mipmap/icon_communities"        android:title="社交圈子"/>    </menu>  </item></menu>

 

注意主题:

<style name="DemoTheme" parent="Theme.AppCompat.Light.NoActionBar">    <item name="colorPrimary">#38A4DE</item>    <item name="colorPrimaryDark">#1B88CA</item>    <item name="colorAccent">#1B88CA</item>  </style>

<application        android:theme="@style/DemoTheme" >      </application>