今天来看一下 NavigationView 的使用,NavigationView 是一个标准的导航菜单,其菜单内容由菜单资源文件来填充,NavigationView 一般和 DrawerLayout
基本布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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">
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
常用属性
android:layout_gravity="start"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/colorNormal"
app:itemBackground="@color/colorBackground"
app:menu="@menu/menu_navigation_view"
app:headerLayout="@layout/head_navigation_layout"
文字选中效果
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPress" android:state_pressed="true"/>
<item android:color="@color/colorCheck" android:state_checked="true"/>
<item android:color="@color/colorNormal"/>
</selector>
app:itemTextColor="@color/select_color_navigation"
//设置菜单项颜色
ColorStateList colorStateList = getResources().getColorStateList(R.color.select_color_navigation)
navigationView.setItemTextColor(colorStateList)
navigationView.setNavigationItemSelectedListener(this);
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Toast.makeText(this,"menu1",Toast.LENGTH_SHORT).show();
break;
}
item.setCheckable(true);
return true;
}
图标与文字间距
public static final int design_navigation_elevation = 0x7f060064;
public static final int design_navigation_icon_padding = 0x7f060065;
public static final int design_navigation_icon_size = 0x7f060066;
public static final int design_navigation_max_width = 0x7f060067;
public static final int design_navigation_padding_bottom = 0x7f060068;
public static final int design_navigation_separator_vertical_padding = 0x7f060069;
<dimen name="design_navigation_icon_padding" tools:override="true">10dp</dimen>
案例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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.support.v7.widget.Toolbar
android:id="@+id/navToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/navDrawLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/head_navigation_layout"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/select_color_navigation"
app:menu="@menu/menu_navigation_view" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
显示效果