底部导航栏实现简单方法BottomTabBar

//导入依赖
implementation 'com.hjm:BottomTabBar:1.1.1'
//布局文件
<com.hjm.bottomtabbar.BottomTabBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottomTabBar"
    android:layout_alignParentBottom="true"></com.hjm.bottomtabbar.BottomTabBar>

//因为需要有多个fragment所以需要创建多个fragment

//声明控件
private BottomTabBar bottomTabBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zhu);
//得到控件
    bottomTabBar = findViewById(R.id.bottomTabBar);
//添加管理者
   bottomTabBar.init(getSupportFragmentManager())
            .setFontSize(10)      //设置字体的大小
            .setImgSize(50,50)       //设置图片的大小
            .setChangeColor(Color.RED,Color.DKGRAY)      //设置颜色的改变
            .addTabItem("首页",R.mipmap.shouye,FirstFragment.class)     //添加文字 图片 fragment
            .addTabItem("分类",R.mipmap.fenlei,FenLeiFragment.class)
            .addTabItem("购物车",R.mipmap.car,CarFragment.class)
            .addTabItem("我的",R.mipmap.wode,MineFragment.class)
            .isShowDivider(true);      //设置显示分配器
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Android 底部导航栏可以使用以下步骤: 1. 在布局文件中添加 BottomNavigationView 控件。 2. 在 Java 文件中获取 BottomNavigationView 的实例,并设置导航栏的选项。 3. 创建多个 Fragment 来作为导航栏的选项内容。 4. 在 Java 文件中设置导航栏选项的监听器,根据选项的不同切换 Fragment。 以下是一个基本的 Android 底部导航栏实现示例代码: XML 布局文件: ``` <RelativeLayout <!-- other views --> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_navigation_view" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation_menu" /> </RelativeLayout> ``` Java 文件: ``` public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigationView = findViewById(R.id.bottom_navigation_view); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.navigation_home: switchFragment(new HomeFragment()); return true; case R.id.navigation_search: switchFragment(new SearchFragment()); return true; case R.id.navigation_notifications: switchFragment(new NotificationFragment()); return true; } return false; } }); // 默认显示 HomeFragment switchFragment(new HomeFragment()); } private void switchFragment(Fragment fragment) { getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit(); } } ``` 其中,我们使用了 FrameLayout 来作为 Fragment 的容器,将 Fragment 切换时使用了 FragmentTransaction。同时,我们使用了 BottomNavigationView 的 setOnNavigationItemSelectedListener 来监听导航栏选项点击事件,并根据不同选项切换不同的 Fragment。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值