欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发中如何使用BottomTabBar实现底部导航页

程序员文章站 2023-11-24 23:04:10
android开发中如何使用bottomtabbar实现底部导航页。 1.导依赖 compile 'com.hjm:bottomtabbar:1.1.1' 2.在xml中定义一下...

android开发中如何使用bottomtabbar实现底部导航页。

1.导依赖

compile 'com.hjm:bottomtabbar:1.1.1'

2.在xml中定义一下该控件

<com.hjm.bottomtabbar.bottomtabbar
    android:id="@+id/bottom_tab_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
com.hjm.bottomtabbar.bottomtabbar>

3.定义每个fragment,这里列举一个


public class onefragment extends fragment{
    @nullable
    @override
    public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
        view view = inflater.inflate(r.layout.fragment1, container, false);
        return view;

    }
}

4.mainactivity中


public class mainactivity extends appcompatactivity {
private bottomtabbar mb;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
      mb=(bottomtabbar)findviewbyid(r.id.bottom_tab_bar);

        mb.init(getsupportfragmentmanager())
                .setimgsize(50,50)
                .setfontsize(8)
                .settabpadding(4,6,10)
                .setchangecolor(color.red,color.dkgray)
                .addtabitem("月光茶人",r.drawable.tab_home, onefragment.class)
                .addtabitem("优惠",r.drawable.tab_topic, twofragment.class)
                .addtabitem("购物车",r.drawable.main_index_cart_normal, threefragment.class)
                .addtabitem("我的",r.drawable.main_index_my_normal, fourfragment.class)
                .isshowdivider(false)
                .setontabchangelistener(new bottomtabbar.ontabchangelistener() {
                    @override
                    public void ontabchange(int position, string name) {

                    }
                });

    }
}
Android开发中如何使用BottomTabBar实现底部导航页

1.导依赖

compile 'com.hjm:bottomtabbar:1.1.1'

2.在xml中定义一下该控件




3.定义每个fragment,这里列举一个


public class onefragment extends fragment{
    @nullable
    @override
    public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) {
        view view = inflater.inflate(r.layout.fragment1, container, false);
        return view;

    }
}

4.mainactivity中


public class mainactivity extends appcompatactivity {
private bottomtabbar mb;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
      mb=(bottomtabbar)findviewbyid(r.id.bottom_tab_bar);

        mb.init(getsupportfragmentmanager())
                .setimgsize(50,50)
                .setfontsize(8)
                .settabpadding(4,6,10)
                .setchangecolor(color.red,color.dkgray)
                .addtabitem("月光茶人",r.drawable.tab_home, onefragment.class)
                .addtabitem("优惠",r.drawable.tab_topic, twofragment.class)
                .addtabitem("购物车",r.drawable.main_index_cart_normal, threefragment.class)
                .addtabitem("我的",r.drawable.main_index_my_normal, fourfragment.class)
                .isshowdivider(false)
                .setontabchangelistener(new bottomtabbar.ontabchangelistener() {
                    @override
                    public void ontabchange(int position, string name) {

                    }
                });

    }
}
Android开发中如何使用BottomTabBar实现底部导航页