第三方组件BottomBar使用

官方地址https://github.com/roughike/BottomBar


在gradle中配置

compile 'com.roughike:bottom-bar:2.0.2'

不知道为什么用2.1以上的版本会配置错误,现在就用这个2.0.1的吧


素材准备:底部栏的几个icon对于大小有要求,24dp,应该在不同资源文件夹下存不同大小的图片,这里为了方便直接用美图秀秀把几个bmp缩小成24*24,直接放在drawable下。注意如果图片太大图片可以正常显示但是下面的文字不会显示。


先在res/xml下定义一个xml文件保存栏中几个tab的属性:

<tabs>
    <tab
        id="@+id/tab_chat"
        icon="@drawable/chat_1"
        title="会话" />
    <tab
        id="@+id/tab_contact"
        icon="@drawable/contact_1"
        title="发现" />
    <tab
        id="@+id/tab_setting"
        icon="@drawable/setting_1"
        title="设置" />
</tabs>
可以看到每个tab基本有
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要添加一个底部导航栏(BottomBar),你可以按照以下步骤进行操作: 1. 首先,在你的项目中创建一个新的布局文件(例如,`activity_main.xml`)并添加一个`LinearLayout`或`RelativeLayout`作为根容器。 2. 在根容器中添加一个`FrameLayout`作为内容容器,用于显示不同的页面内容。比如: ```xml <FrameLayout android:id="@+id/content_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> ``` 3. 在底部导航栏之上,添加一个`LinearLayout`或`RelativeLayout`作为底部导航栏容器。比如: ```xml <LinearLayout android:id="@+id/bottom_bar_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 在这里添加底部导航栏的按钮和其他相关视图 --> </LinearLayout> ``` 4. 在`MainActivity`或其他相关的活动中,找到底部导航栏容器的引用,并添加导航按钮。你可以使用`Button`、`ImageView`或其他适合的视图来表示每个导航按钮。例如: ```java LinearLayout bottomBarContainer = findViewById(R.id.bottom_bar_container); Button homeButton = new Button(this); homeButton.setText("Home"); homeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击 Home 按钮的逻辑 } }); bottomBarContainer.addView(homeButton); ``` 5. 根据需要,为每个按钮添加点击事件处理逻辑,并在点击时更新内容容器中的页面。你可以使用`FragmentManager`来切换不同的页面。例如: ```java FragmentManager fragmentManager = getSupportFragmentManager(); homeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment homeFragment = new HomeFragment(); fragmentManager.beginTransaction() .replace(R.id.content_container, homeFragment) .commit(); } }); ``` 6. 重复步骤4和步骤5,为每个导航按钮添加相应的视图和逻辑。 这样,你就可以通过底部导航栏来切换不同的页面了。记得根据你的需求进行样式和布局的调整。祝你好运!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值