1.首先在build.gradle 中添加 implementation 'com.google.android.material:material:1.2.0'
2.在 gradle.properties 文件添加
android.useAndroidX=true
完成上述两步后开始写代码
1.在activi_main.xml布局文件中添加
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/purple1"
app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"
app:menu="@menu/main" />
其中 @drawable/selector 文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:color="@android:color/black"/>
<item
android:state_selected="true"
android:color="@android:color/white"/>
</selector>
其中@meau/main代码如下
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.exerdemolinechart.MainActivity" >
<item
android:id="@+id/Home"
android:icon="@drawable/home"
android:title="Home"/>
<item
android:id="@+id/Dashboard"
android:icon="@drawable/dashboard"
android:title="Dashboard"/>
<item
android:id="@+id/About"
android:icon="@drawable/about"
android:title="About"/>
</menu>
tips: @drable/home 文件由图所示步骤自动生成 (about dashboard同理)
2.新建java类about.java (和dashboard.java)
3.在about.xml(和dashboard.xml)中
添加相同代码
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/purple1"
app:itemIconTint="@drawable/selector"
app:itemTextColor="@drawable/selector"
app:menu="@menu/main" />
4.在mainactivity.java中添加如下代码
BottomNavigationView bottomNavigationView = findViewById(R.id.navi);
bottomNavigationView.setSelectedItemId(R.id.Home);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.About:
startActivity(new Intent(getApplicationContext(), about.class));
overridePendingTransition(0, 0);
return true;
case R.id.Dashboard:
startActivity(new Intent(getApplicationContext(), dashboard.class));
overridePendingTransition(0, 0);
return true;
case R.id.Home:
return true;
}
return false;
}
});
}
5.在about.java中添加如下代码 (同理在dashboard.java中添加)
BottomNavigationView bottomNavigationView = findViewById(R.id.navi);
bottomNavigationView.setSelectedItemId(R.id.About);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.Home:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
overridePendingTransition(0, 0);
return true;
case R.id.Dashboard:
startActivity(new Intent(getApplicationContext(), dashboard.class));
overridePendingTransition(0, 0);
return true;
case R.id.About:
return true;
}
return false;
}
});
}
别忘了在在AndroidManifest里注册新建的Activity!!!!
效果图: