写一个包含fragment的安卓程序实现QQ的底部点击切换功能
时间: 2024-05-04 07:20:19 浏览: 148
使用Fragment实现底部菜单的切换
5星 · 资源好评率100%
以下是一个简单的实现底部点击切换功能的安卓程序,使用了Fragment。在这个示例中,我们实现了QQ的底部导航栏的功能。
1. 在activity_main.xml文件中添加BottomNavigationView和FrameLayout:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation" />
<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="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/navigation" />
</RelativeLayout>
```
2. 在menu文件夹中创建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>
```
3. 创建三个Fragment,分别对应底部导航栏的三个选项,例如HomeFragment、DashboardFragment和NotificationsFragment。
4. 在MainActivity中实现底部导航栏的点击事件,并切换到相应的Fragment:
```
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private FragmentManager fragmentManager;
private HomeFragment homeFragment;
private DashboardFragment dashboardFragment;
private NotificationsFragment notificationsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
fragmentManager.beginTransaction().replace(R.id.frame_container, homeFragment).commit();
return true;
case R.id.navigation_dashboard:
fragmentManager.beginTransaction().replace(R.id.frame_container, dashboardFragment).commit();
return true;
case R.id.navigation_notifications:
fragmentManager.beginTransaction().replace(R.id.frame_container, notificationsFragment).commit();
return true;
}
return false;
}
});
fragmentManager = getSupportFragmentManager();
homeFragment = new HomeFragment();
dashboardFragment = new DashboardFragment();
notificationsFragment = new NotificationsFragment();
fragmentManager.beginTransaction().replace(R.id.frame_container, homeFragment).commit();
}
}
```
这样,当用户点击底部导航栏的选项时,就会切换到相应的Fragment。
阅读全文