📅  最后修改于: 2023-12-03 15:24:06.372000             🧑  作者: Mango
在Android应用中,底部导航是非常常见的设计模式。它可以使用户轻松地浏览应用程序的不同部分,提高用户体验。在本文中,将介绍如何使用Activity实现底部导航。
Android提供了一个组件BottomNavigationView,它可以轻松创建和管理底部导航。以下是一些简单的步骤来使用BottomNavigationView:
在应用程序build.gradle文件中添加以下依赖关系:
implementation 'com.google.android.material:material:1.2.1'
在布局文件中添加BottomNavigationView组件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 添加一个FrameLayout来容纳Fragment -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintTop_toTopOf="parent" />
<!-- 添加BottomNavigationView -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@color/black"
app:itemTextColor="@color/black"
app:menu="@menu/bottom_navigation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
在res目录下创建menu文件夹(如果没有),在其中创建bottom_navigation.xml文件,如下所示:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 添加底部菜单项 -->
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
</menu>
在应用程序中使用Fragment可以创建多个视图,因此可以通过点击底部菜单项来更改视图。以下是一个简单的示例:
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
textView.setText("Home Fragment");
return root;
}
}
public class DashboardFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final TextView textView = root.findViewById(R.id.text_dashboard);
textView.setText("Dashboard Fragment");
return root;
}
}
public class NotificationsFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
final TextView textView = root.findViewById(R.id.text_notifications);
textView.setText("Notifications Fragment");
return root;
}
}
在Activity中,需要实例化BottomNavigationView组件,然后为其添加OnNavigationItemSelectedListener侦听器,如下所示:
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private FragmentManager fragmentManager = getSupportFragmentManager();
private Fragment currentFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 设置 BottomNavigationView
BottomNavigationView navView = findViewById(R.id.navigation);
navView.setOnNavigationItemSelectedListener(this);
//添加默认的Fragment
selectFragment(R.id.navigation_home);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectFragment(item.getItemId());
return true;
}
//根据Menu项的Id来选择对应的Fragment
private void selectFragment(int itemId) {
Fragment fragment = null;
switch (itemId) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
}
if (fragment != null) {
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}
}
使用BottomNavigationView是实现底部导航的最佳方式之一。以上是简单的步骤来使用BottomNavigationView实现底部导航,可使用户可以轻松地浏览应用程序的不同部分。