赞
踩
compile 'com.android.support:design:28.+'
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent"> <android.support.design.widget.NavigationView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/mainNavigationView1" app:menu="@menu/menu" android:layout_gravity="center"/> </FrameLayout>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:icon="@drawable/download" android:title="item1"/> <item android:id="@+id/item2" android:icon="@drawable/time" android:title="item2"/> <item android:id="@+id/item3" android:icon="@drawable/ic_launcher" android:title="item3"/> </menu>
(与DrawerLayout结合使用时 ,我们一般都会把NavigationView设置为单选)
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/item1" android:icon="@drawable/download" android:title="item1"/> <item android:id="@+id/item2" android:icon="@drawable/time" android:title="item2"/> <item android:id="@+id/item3" android:icon="@drawable/ic_launcher" android:title="item3"/> </group> </menu>
NavigationView mNavigationView=(NavigationView) findViewById(R.id.mainNavigationView1); mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){ @Override public boolean onNavigationItemSelected(MenuItem p1) { switch (p1.getItemId()) { case R.id.item1: { Toast.makeText(MainActivity.this,"item1",Toast.LENGTH_SHORT).show(); } break; case R.id.item2: { Toast.makeText(MainActivity.this,"item2",Toast.LENGTH_SHORT).show(); } break; case R.id.item3: { Toast.makeText(MainActivity.this,"item3",Toast.LENGTH_SHORT).show(); }} //注意这里要返回true,否则单选无效 return true; } });
1.通过主题修改
<item name="colorPrimary">#0DB8F7</item>
2.通过属性修改
//文本颜色
app:itemTextColor="@drawable/selector_color"
//icon颜色
app:itemIconTint="@drawable/selector_color"
selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--按下时-->
<item android:color="#7516B5" android:state_pressed="true"/>
<!--选中-->
<item android:color="#3ECB13" android:state_checked="true"/>
<!--默认-->
<item android:color="#D9D9D9"/>
</selector>
不想用也可以设置为null
即:不对图标进行渲染,使用图标原来的颜色
3.在java代码中修改
mNavigationView.setItemIconTintList(getResources().getColorStateList(R.drawable.selector_text));
mNavigationView.setItemTextColor(getResources().getColorStateList(R.drawable.selector_text));
在NavigationView添加属性
app:headerLayout="@layout/nav_header"
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:background="#0DB8F7"
android:gravity="center">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="登录"
android:id="@+id/navheaderButton1"/>
</LinearLayout>
mNavigationView.getHeaderView(0).findViewById()
来获取按钮Button bt= (Button) mNavigationView.getHeaderView(0).findViewById(R.id.navheaderButton1);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View p1)
{
Toast.makeText(MainActivity.this,"你点击了按钮",Toast.LENGTH_SHORT).show();
}
});
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.design.widget.BottomNavigationView
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:menu="@menu/menu"
android:layout_gravity="center"
android:id="@+id/mainBottomNavigationView1"/>
</FrameLayout>
BottomNavigationView mNavigationView=(BottomNavigationView) findViewById(R.id.mainBottomNavigationViewView1); mNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){ @Override public boolean onNavigationItemSelected(MenuItem p1) { switch (p1.getItemId()) { case R.id.item1: { Toast.makeText(MainActivity.this,"item1",Toast.LENGTH_SHORT).show(); } break; case R.id.item2: { Toast.makeText(MainActivity.this,"item2",Toast.LENGTH_SHORT).show(); } break; case R.id.item3: { Toast.makeText(MainActivity.this,"item3",Toast.LENGTH_SHORT).show(); }} //注意这里要返回true,否则单选无效 return true; } });
本文到此结束
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。