BottomBar底部导航实现

首先主要实现的功能就BottomBar的底部导航功能,还有Fragment的联动,如上图。

首先我们先导依赖

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

然后在我们的res文件夹下,新建一个xml文件,写上我们底部要实现的图片和文字

<?xml version="1.0" encoding="utf-8"?>
<tabs>

    <tab
        id="@+id/tab_yi"
        icon="@mipmap/found"
        title="Recents"
        barColorWhenSelected="#FE3D81" />

    <tab
        id="@+id/tab_er"
        title="Favorites"
        icon="@mipmap/fancy"
        barColorWhenSelected="#5D3C35" />

    <tab
        id="@+id/tab_san"
        icon="@mipmap/special"
        title="Nearby"
        barColorWhenSelected="#7B1FA2" />

    <tab
        id="@+id/tab_si"
        icon="@mipmap/my"
        title="Restaurants"
        barColorWhenSelected="#FF9800" />
</tabs>

这是我的们布局里的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.xinyun.mybar.MainActivity">

    <FrameLayout
        android:id="@+id/contentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
 
    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:bb_tabXmlResource="@xml/bottombar_menu"/>

</LinearLayout>

最后就是我们的代码了

public class MainActivity extends AppCompatActivity {

    public BottomBar bottomBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //去掉头部
        getSupportActionBar().hide();
        //找控件
        bottomBar = findViewById(R.id.bottomBar);
        //Fragment关联
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {
                Object ob=null;
                switch (tabId){
                    case R.id.tab_yi:
                       ob = new FragmentOne();
                        break;
                    case R.id.tab_er:
                        ob = new FragmentEr();
                        break;
                    case R.id.tab_san:
                        ob = new FragmentSan();
                        break;
                    case R.id.tab_si:
                        ob = new FragmentSi();
                        break;
                        default:
                            break;
                }getSupportFragmentManager().beginTransaction().replace(R.id.contentContainer,(Fragment) ob).commit();
            }
        });

    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值