基本使用
1.引入依赖
implementation 'com.android.support:design:28.0.0'
2.新建menu文件(在res下新建menu资源文件夹,然后为bottomnavigationview创建menu文件)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/homepage"
android:enabled="true"
app:showAsAction="ifRoom"
android:icon="@drawable/homepage"
android:title="主页"/>
<item android:id="@+id/knowledge"
android:enabled="true"
app:showAsAction="ifRoom"
android:icon="@drawable/homepage"
android:title="知识体系"/>
<item android:id="@+id/navigate"
android:enabled="true"
app:showAsAction="ifRoom"
android:icon="@drawable/homepage"
android:title="导航"/>
<item android:id="@+id/wechat"
android:enabled="true"
app:showAsAction="ifRoom"
android:icon="@drawable/homepage"
android:title="公众号"/>
<item android:id="@+id/project"
android:enabled="true"
app:showAsAction="ifRoom"
android:icon="@drawable/homepage"
android:title="项目"/>
</menu>
3.定义选中和非选中颜色(注意,这里使用的是android:color,当时我使用的是android:drawable 怎么都实现不了想要的效果)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:color="@color/bottom_navigation_view_text_color_unchecked"/>
<item android:state_checked="true" android:color="@color/bottom_navigation_view_text_color_checked"/>
</selector>
-
在布局文件中定义组件
<android.support.design.widget.BottomNavigationView app:elevation="16dp" android:id="@+id/bnv_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:windowBackground" app:labelVisibilityMode="labeled" app:itemTextColor="@drawable/bottom_navigation_view_color_selector" app:itemIconTint="@drawable/bottom_navigation_view_color_selector" app:layout_constraintBottom_toBottomOf="parent" app:menu="@menu/menu_bottom_navigation_view"> </android.support.design.widget.BottomNavigationView>
实现不同的效果
1只有图标
只需要设置 labelVisibilityMode="unlabeled"
2 图标文字每一项都完全显示 并且去除移动动画
设置labelVisibilityMode="labeled"
3 固定字体大小(只需要在dimens文件中添加以下两行就行了 )
方式:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--<dimen name="design_bottom_navigation_active_text_size">12sp</dimen>-->
<!--<dimen name="design_bottom_navigation_text_size">12sp</dimen>-->
</resources>
4.是指图标与label之间的间隔以及导航栏的高度
同样是在dimens文件中
<!-- icon与text间隔大小 -->
<!--<dimen tools:override="true" name="design_bottom_navigation_margin">20dp</dimen>-->
<!-- 导航栏高度 -->
<!--<dimen tools:override="true" name="design_bottom_navigation_height">90dp</dimen>-->
5.去掉水波纹
app:itemBackground="@null"
选中事件
package com.zengqiang.wanandroid;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.container)
FrameLayout mContainer;
@BindView(R.id.bnv_main)
BottomNavigationView mBnvMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initBottomNavigationView();
}
private void initBottomNavigationView() {
mBnvMain.setOnNavigationItemSelectedListener(item -> {
switch (item.getItemId()) {
case R.id.homepage:
Toast.makeText(this, "homepage", Toast.LENGTH_SHORT).show();
break;
case R.id.knowledge:
Toast.makeText(this, "knowledge", Toast.LENGTH_SHORT).show();
break;
case R.id.navigate:
Toast.makeText(this, "navigate", Toast.LENGTH_SHORT).show();
break;
case R.id.project:
Toast.makeText(this, "project", Toast.LENGTH_SHORT).show();
break;
case R.id.wechat:
Toast.makeText(this, "wechat", Toast.LENGTH_SHORT).show();
break;
}
return true;
});
}
}