安卓开发——做一个简单的底部导航栏

​​​​​​

一、思维导图

1.思维导图大概思路

 

二、使用步骤

1.写控件

在R.layout.activity_main 创建Fragment和BottomNavigationView俩个控件

说明:

BottomNavigationView

1).在BottomNavigationView中需要写一个显示底部图标和文字的菜单


        app:menu="@menu/menu

<menu 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"
    tools:context="com.example.city.MainActivity">
    <item
        android:id="@+id/city_home"

        android:orderInCategory="100"
        android:title="首页"
        android:icon="@drawable/home" />
    <!--id用来表示这个item的标识 后面需要用它绑定点击时间
    tltle用来表示上面显示的文字
    icon用来表示图标
    -->
    <item
        android:id="@+id/city_service"
        android:orderInCategory="100"
        android:title="全部服务"
        android:icon="@drawable/fuwu"
        app:showAsAction="never" />
    <item
        android:id="@+id/city_dangjian"
        android:orderInCategory="100"
        android:title="智慧党建"
        android:icon="@drawable/study"
        app:showAsAction="never" />
    <item
        android:id="@+id/city_news"
        android:orderInCategory="100"
        android:icon="@drawable/news"
        android:title="新闻"
        app:showAsAction="never" />
    <item
        android:id="@+id/city_geren"
        android:orderInCategory="100"
        android:icon="@drawable/geren"
        android:title="个人中心"
        app:showAsAction="never" />

    <!--    首页、全部服务、智慧党建、新闻、个人中心-->
</menu>


   
R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_view"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        app:menu="@menu/menu"
        android:layout_height="wrap_content"/>


</RelativeLayout>

2.创建需要的Fragment

3.在MainActivity创建一个方法对Fragment进行实例化

对Fragment进行实例化然后对  FragmentManager进行获取

 private void initFragemnt() {
        dangFragment = new DangFragment();
        mainFragment = new MainFragment();
        myFragment = new MYFragment();
        serviceFragment = new ServiceFragment();

        newsFragment = new NewsFragment();
        fragmentManager =getSupportFragmentManager();
Swich(mainFragment);

    }

4.创建一个方法 绑定创建的Fragment

   private void Swich(Fragment targrFragment) {
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_pav,targrFragment);
        fragmentTransaction.commit();
        }

5.对创建的Fragment和下面的BottomNavigationView进行绑定

  BottomNavigationView bnv= findViewById(R.id.boomview);

        bnv.setOnNavigationItemSelectedListener(item -> {



            Log.d(TAG,"title>"+item.getTitle());
            if(item.getItemId()==R.id.city_home){
                Log.d(TAG,"title>"+item.getItemId());
              Swich(mainFragment);
            }else if(item.getItemId()==R.id.city_news){
                Log.d(TAG,"title>"+item.getItemId());
                Swich(newsFragment);
            }else if(item.getItemId()==R.id.city_service){
                Swich(serviceFragment);
                Log.d(TAG,"title>"+item.getItemId());
            }else if(item.getItemId()==R.id.city_dangjian){
                Log.d(TAG,"title>"+item.getItemId());
                Swich(dangFragment);
            }else if(item.getItemId()==R.id.city_geren){
                Log.d(TAG,"title>"+item.getItemId());
                Swich(myFragment);
            }

            return true;
        });


总结

通过给的思维导图了解思路

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是Android Studio中创建含有底部导航栏的页面的步骤: 1. 打开Android Studio并创建一个新项目。 2. 在项目中创建一个新的Activity。 3. 打开xml布局文件,添加一个底部导航栏。可以使用Google提供的Material Design组件库中的BottomNavigationView。 4. 在Activity中,使用findViewByID方法找到布局文件中的BottomNavigationView对象。 5. 使用BottomNavigationView的setOnNavigationItemSelectedListener方法设置导航栏的选项被选中时的监听器。 6. 在监听器中,根据选中的选项,使用Intent启动对应的Activity。 以下是一个简单的示例代码: 布局文件 `activity_main.xml`: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_navigation" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation_menu" /> </RelativeLayout> ``` `MainActivity.java` 文件: ```java public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; private FrameLayout frameLayout; 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.bottom_navigation); frameLayout = findViewById(R.id.frameLayout); // 初始化Fragment homeFragment = new HomeFragment(); dashboardFragment = new DashboardFragment(); notificationsFragment = new NotificationsFragment(); // 默认显示HomeFragment setFragment(homeFragment); // 设置底部导航栏的监听器 bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.nav_home: setFragment(homeFragment); return true; case R.id.nav_dashboard: setFragment(dashboardFragment); return true; case R.id.nav_notifications: setFragment(notificationsFragment); return true; } return false; } }); } // 切换Fragment private void setFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frameLayout, fragment); fragmentTransaction.commit(); } } ``` `bottom_navigation_menu.xml` 文件: ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" app:showAsAction="ifRoom" /> <item android:id="@+id/nav_dashboard" android:icon="@drawable/ic_dashboard" android:title="Dashboard" app:showAsAction="ifRoom" /> <item android:id="@+id/nav_notifications" android:icon="@drawable/ic_notifications" android:title="Notifications" app:showAsAction="ifRoom" /> </menu> ``` 在这个示例中,我们创建了一个包含三个Fragment的底部导航栏,分别是HomeFragment、DashboardFragment和NotificationsFragment。当用户点击导航栏中的选项时,我们会使用Intent启动对应的Fragment。你可以根据自己的需求修改/扩展代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力进大厂的小菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值