implementation 'com.google.android.material:material:1.2.1'
<androidx.constraintlayout.widget.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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@android:color/white" app:layout_constraintBottom_toBottomOf="parent" app:menu="@menu/menu2" /> </androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity { private TextView mTextMessage; private static final String TAG = "MainActivity"; BottomNavigationView navigation;//底部导航栏对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { navigation = (BottomNavigationView)findViewById(R.id.navigation); // 重新点击监听(即点击目前选中的Tab时触发) navigation.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() { @Override public void onNavigationItemReselected(@NonNull MenuItem item) { Log.e("ee","Reselected Item:"+item.getTitle()); } }); navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Log.e("ee","Selected Item:"+item.getTitle()); onTabSelected(item.getItemId()); return true; } }); onTabSelected(R.id.navigation_home); }
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/navigation_home" android:icon="@drawable/bottom1" android:title="home" /> <item android:id="@+id/navigation_dashboard" android:icon="@drawable/bottom2" android:title="dashboard" /> <item android:id="@+id/navigation_notifications" android:icon="@drawable/bottom3" android:title="notifications" /> <item android:id="@+id/navigation_setting" android:icon="@drawable/bottom4" android:title="share" /> </menu>