原生BottomNavigationView的使用
首先注入design包
然后xml里面
<android.support.design.widget.BottomNavigationView
android:background="@color/activity_bg"
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="labeled"
app:itemBackground="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/nabigation" />
文件里面的meun 在res目录下创建meun文件加然后创建nabigation.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/navigation_phone"
android:icon="@drawable/adress"
app:showAsAction="ifRoom"
android:title="@string/adress" />
<item
android:id="@+id/navigation_home"
android:icon="@drawable/home"
app:showAsAction="ifRoom"
android:title="@string/home" />
<item
android:id="@+id/navigation_found"
android:icon="@drawable/unfound"
app:showAsAction="ifRoom"
android:title="@string/found" />
</menu>
activity中
private void initBottomNativiBar() {
bottomNavigationBar.setItemIconTintList(null);
Resources resource=getBaseContext().getResources();
ColorStateList csl=resource.getColorStateList(R.color.bottom_text_color);
bottomNavigationBar.setItemTextAppearanceActive(R.style.bottom_selected_text);
bottomNavigationBar.setItemTextAppearanceInactive(R.style.bottom_normal_text);
bottomNavigationBar.setItemTextColor(csl);
bottomNavigationBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
FragmentManager fm = ActivityMain.this.getSupportFragmentManager();
//开启事务
FragmentTransaction transaction = fm.beginTransaction();
resetToDefaultIcon();//图标全部默认
switch (menuItem.getItemId()) {
case R.id.navigation_phone:
if (fragmentAdress == null) {
fragmentAdress = FragmentAdress.newInstance();
}
menuItem.setIcon(R.drawable.un_adress);
menuItem.setChecked(true);
if (transaction==null)transaction =fm.beginTransaction();
transaction.replace(R.id.fragment, fragmentAdress).commit();
break;
case R.id.navigation_home:
if (fragmentHome == null) {
fragmentHome = FragmentHome.newInstance();
}
menuItem.setIcon(R.drawable.un_home);
menuItem.setChecked(true);
if (transaction==null)transaction =fm.beginTransaction();
transaction.replace(R.id.fragment, fragmentHome).commit();
break;
case R.id.navigation_found:
if (fragmentFound == null) {
fragmentFound = fragmentFound.newInstance();
}
menuItem.setIcon(R.drawable.found);
menuItem.setChecked(true);
if (transaction==null)transaction =fm.beginTransaction();
transaction.replace(R.id.fragment, fragmentFound).commit();
break;
}
return false;
}
});
//默认显示出来FragmentAdress
FragmentManager fm = ActivityMain.this.getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
if (fragmentHome == null) {
fragmentHome = FragmentHome.newInstance();
}
transaction.replace(R.id.fragment, fragmentHome).commit();
bottomNavigationBar.getMenu().getItem(1).setChecked(true);
bottomNavigationBar.getMenu().getItem(1).setIcon(R.drawable.un_home);
}
private void resetToDefaultIcon() {
MenuItem adress = bottomNavigationBar.getMenu().findItem(R.id.navigation_phone);
adress.setIcon(R.drawable.adress);
MenuItem home = bottomNavigationBar.getMenu().findItem(R.id.navigation_home);
home.setIcon(R.drawable.home);
MenuItem found = bottomNavigationBar.getMenu().findItem(R.id.navigation_found);
found.setIcon(R.drawable.unfound);
}
style文件中
<style name="bottom_normal_text">
<item name="android:textColor">@color/font_hint</item>
<item name="android:textSize">14sp</item>
</style>
<!--选中的样式为了去除bottom动画-->
<style name="bottom_selected_text">
<item name="android:textColor">@color/colorPrimary</item>
<item name="android:textSize">14sp</item>
</style>