用fragment组件制作一个底部导航栏
时间: 2024-05-07 15:16:49 浏览: 107
好的,可以使用BottomNavigationView组件来实现底部导航栏。首先在布局文件中添加BottomNavigationView组件,然后使用FragmentTransaction将对应的Fragment添加到页面中。以下是一个简单的实现示例:
1. 在布局文件中添加BottomNavigationView组件
```
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation_menu" />
```
2. 在Activity中设置BottomNavigationView的选项卡并添加对应的Fragment
```
public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager = getSupportFragmentManager();
private Fragment fragmentHome = new HomeFragment();
private Fragment fragmentNews = new NewsFragment();
private Fragment fragmentSettings = new SettingsFragment();
private BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化BottomNavigationView
navigationView = findViewById(R.id.navigation_view);
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.menu_home:
replaceFragment(fragmentHome);
return true;
case R.id.menu_news:
replaceFragment(fragmentNews);
return true;
case R.id.menu_settings:
replaceFragment(fragmentSettings);
return true;
}
return false;
}
});
// 首次加载显示默认Fragment
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragmentHome).commit();
}
private void replaceFragment(Fragment fragment) {
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
}
}
```
3. 创建对应的Fragment
```
public class HomeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
}
public class NewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
return view;
}
}
public class SettingsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
return view;
}
}
```
4. 最后在menu文件夹下创建navigation_menu.xml文件
```
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_home"
android:icon="@drawable/ic_home"
android:title="Home" />
<item
android:id="@+id/menu_news"
android:icon="@drawable/ic_news"
android:title="News" />
<item
android:id="@+id/menu_settings"
android:icon="@drawable/ic_settings"
android:title="Settings" />
</menu>
```
这样,就可以在底部导航栏中添加无数选项卡,并且在点击对应选项卡时自动切换对应的Fragment了。
阅读全文