NavigationView common attributes and basic usage
Effect picture:
Common attributes:
android:layout_gravity
: left/start
Means sliding out from the left, right
means sliding out from the right
android:background
: Background color (except header
background color)
app:itemBackground
: Sub-background color (the background color of each sub-block)
app:headerLayout
: Set the head layout
app:menu
: Setting options menu ( menu
file under menu
file)
app:itemTextColor
: Set the font color (here is color
the one under the file <selector>
)
app:itemTextAppearance
:Set the size of the text style ( style.xml
defined in)
<com.google.android.material.navigation.NavigationView
android:id="@+id/nv_sides_lip"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="@layout/layout_sidelip"
app:menu="@menu/menu_sidelip"
app:itemTextColor="@color/nv_item_text"
android:background="@drawable/bac_top_user" app:itemTextAppearance="@style/navigation_item_text_size"
/>
How to modify the icon size, text size, icon and text spacing of Menu in NavigationView
1. Modify 图标大小
:
In style.xml
the definition of a <dimen>
rewrite attribute design_navigation_icon_size
. xml
File header needs to be importedtools
<dimen name="design_navigation_icon_size" tools:override="true">图标具体的大小</dimen>
2. Modify 文字大小
:
In style.xml
a defined<style>
<style name="navigation_item_text_size">
<item name="android:textSize">文字具体的大小</item>
</style>
3. Modify the space between the icon and the text:
In style.xml
the definition of a <dimen>
rewrite attribute design_navigation_icon_padding
. The header of the xml file needs to be importedtools
<dimen name="design_navigation_icon_padding" tools:override="true">具体间距大小</dimen>
Basic usage (Java code):
private DrawerLayout dl_sides_lip; // 侧滑栏布局
private NavigationView nv_sides_lip; // 侧滑栏内布局
dl_sides_lip = findViewById(R.id.dl_sideslip_layout);
nv_sides_lip = findViewById(R.id.nv_sides_lip);
// 设置监听器,NavigationView.OnNavigationItemSelectedListener接口
nv_sides_lip.setNavigationItemSelectedListener(this);
// 保留原 icon 的颜色,不然自己图标的颜色都不生效
nv_sides_lip.setItemIconTintList(null);
// 实现接口方法
/**
* 侧滑栏中 NavigationView 的子项点击事件
* @param menuItem 菜单子项
* @return
*/
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nv_about:
Toast.makeText(getContext(),menuItem.getTitle().toString(),Toast.LENGTH_SHORT).show();
// 关闭侧滑栏
dl_sides_lip.closeDrawer(Gravity.LEFT);
return true;
case R.id.nv_1:
Toast.makeText(getContext(),menuItem.getTitle().toString(),Toast.LENGTH_SHORT).show();
dl_sides_lip.closeDrawer(Gravity.LEFT);
return true;
case R.id.nv_2:
Toast.makeText(getContext(),menuItem.getTitle().toString(),Toast.LENGTH_SHORT).show();
dl_sides_lip.closeDrawer(Gravity.LEFT);
return true;
default:
break;
}
return false;
}
Where the layout:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/dl_sideslip_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<!-- 内容区-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
<include layout="@layout/player" />
</RelativeLayout>
<!-- 左边侧滑菜单区-->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nv_sides_lip"
android:layout_width="350dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@drawable/bac_1"
app:headerLayout="@layout/layout_sidelip"
app:itemTextAppearance="@style/navigation_item_text_size"
app:itemTextColor="@color/nv_item_text"
app:menu="@menu/menu_sidelip" />
</androidx.drawerlayout.widget.DrawerLayout>
layout_sidelip.xml
<?xml version="1.0" encoding="utf-8"?>
<!--侧滑栏NavigationView头部-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!--圆形图片,第三方库-->
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/civ_user_pic"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/ig_2018"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
app:civ_border_width="1dp"
app:civ_border_color="#ebd299"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="Every little helps "
android:layout_toRightOf="@+id/civ_user_pic"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:textColor="#FF82AB"
android:textStyle="bold"
/>
</RelativeLayout>