bottomBar底部导航

本文介绍了一种使用BottomBar库实现Android应用底部导航的方法。通过配置XML文件定义导航项,并在MainActivity中设置监听器响应不同的页面切换。文章提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//依赖

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

//xml布局

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    android:layout_above="@+id/bottomBar" />

<com.roughike.bottombar.BottomBar
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_alignParentBottom="true"
    app:bb_tabXmlResource="@xml/bottombar_img"

    />


//在res/xml/bottombar_img中复制

<?xml version="1.0" encoding="utf-8"?>
 <tabs>
    <tab
        id="@+id/tab_shouye"
        title="首页"
        icon="@drawable/shouye"
        />
    <tab
        id="@+id/tab_all"
        title="分类"
        icon="@drawable/all"

        />
    <tab
        id="@+id/tab_cart"
        title="购物车"
        icon="@drawable/cart"

        />
    <tab
        id="@+id/tab_account"
        title="我的"
        icon="@drawable/account"
        />
</tabs>

//主页面

public class MainActivity extends AppCompatActivity {

    private BottomBar bottomBar;
    private FrameLayout frameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        //frameLayout = findViewById(R.id.frame_layout);
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {

            private Fragment2 fragment2;
            private Fragment1 fragment1;

            @Override
            public void onTabReSelected(int tabId) {
                Object ob=null;
                switch (tabId){
                    case R.id.tab_shouye:
                        ob = new Fragment1();
                        break;
                    case R.id.tab_all:
                        ob = new Fragment2();

                }
                getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, (Fragment) ob).commit();
            }
        });
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值