导读:
使用 Kotlin 和 BottomNavigationBar 实现Android底部导航栏
1. BottomNavigationBar - Kotlin 下载:
BottomNavigationBar-KotlinDemo
2.使用方式:
2.1 gradle(Module)添加依赖库:(FIle -> Project Structure -> app -> Dependencies)
implementation 'com.ashokvarma.android:bottom-navigation-bar:2.0.2'
2.2 BottomNavigationBar布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lijiangbing.bottomnavigationbar.MainActivity">
<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"></com.ashokvarma.bottomnavigation.BottomNavigationBar>
<!--留给Fragment使用-->
<FrameLayout
android:id="@+id/fl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
2.3 MainActivity 使用 BottomNavigationBar
class MainActivity : AppCompatActivity(),BottomNavigationBar.OnTabSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
with(bottom_bar){
//设置显示模式,必须在初始化完成之前设置
setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
setMode(BottomNavigationBar.MODE_FIXED)
addItem(BottomNavigationItem(R.drawable.icon_wechat_g,R.string.nv_bottom).setActiveColorResource(R.color.colorPrimaryDark).setBadgeItem(TextBadgeItem().setText("2")));
addItem(BottomNavigationItem(R.drawable.icon_wechat_g,R.string.nv_bottom).setActiveColorResource(R.color.colorPrimaryDark));
addItem(BottomNavigationItem(R.drawable.icon_wechat_g,R.string.nv_bottom).setActiveColorResource(R.color.colorPrimaryDark));
addItem(BottomNavigationItem(R.drawable.icon_wechat_g,R.string.nv_bottom).setActiveColorResource(R.color.colorPrimaryDark));
setFirstSelectedPosition(0);// 默认选中 的item
initialise();
}
bottom_bar.setTabSelectedListener(this);
// 设置默认 Fragment
val manager = getSupportFragmentManager();
val transaction = manager.beginTransaction();
transaction.replace(R.id.fl,fragment("first"));
transaction.commit();
}
//复写点击监听,替换Fragment
override fun onTabSelected(position: Int) {
val manager = getSupportFragmentManager();
val transaction = manager.beginTransaction();
when (position){
0 -> {
transaction.replace(R.id.fl, fragment("first"))
transaction.commit();
}
1 -> {
transaction.replace(R.id.fl, fragment("second"));
transaction.commit();
}
2 -> {
transaction.replace(R.id.fl, fragment("third"));
transaction.commit();
}
else -> {
transaction.replace(R.id.fl, fragment("last"));
transaction.commit();
}
}
}
override fun onTabUnselected(position: Int) {
}
override fun onTabReselected(position: Int) {
}
}
2.4 Fragment 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/tv_titleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragement"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>