底部导航切换

一:导入底部导航依赖

implementation 'com.hjm:BottomTabBar:1.1.2'

二:xml布局

<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=".view.activity.MainActivity">

    <com.hjm.bottomtabbar.BottomTabBar
        xmlns:hjm="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        hjm:tab_divider_background="#FF0000"
        hjm:tab_divider_height="5dp"
        hjm:tab_font_size="6sp"
        hjm:tab_img_font_padding="0dp"
        hjm:tab_img_height="30dp"
        hjm:tab_img_width="30dp"
        hjm:tab_isshow_divider="true"
        hjm:tab_padding_bottom="5dp"
        hjm:tab_padding_top="8dp"
        hjm:tab_selected_color="#000000"
        hjm:tab_unselected_color="@color/colorPrimary"></com.hjm.bottomtabbar.BottomTabBar>

</LinearLayout>

三:MainActivity代码

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bottom_tab_bar)
    BottomTabBar bottomTabBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        dibu();
    }

    private void dibu() {
        bottomTabBar.init(getSupportFragmentManager())
                .setImgSize(26,26)
                .setFontSize(14)
                .setTabPadding(4,6,10)
                .setChangeColor(Color.RED,Color.DKGRAY)
                .addTabItem("首页",R.drawable.home,HomeFragment.class)
                .addTabItem("分类", R.drawable.classily, ClassifyFragment.class)
                .addTabItem("发现", R.drawable.sou, MessageFragment.class)
                .addTabItem("购物车", R.drawable.shop, ShopFragment.class)
                .addTabItem("我的", R.drawable.my, MyFragment.class)
                .setTabBarBackgroundColor(Color.WHITE)
                .isShowDivider(false);
    }
}

四:创建相关fragment,例如:MyFragment

1.创建onCreateView方法

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //创建相关布局
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        return view;
    }
}

注:方法在onCreateView外面写,如需初始化其它类则在 return view 前面写

猜你喜欢

转载自blog.csdn.net/gylgww/article/details/83893304
今日推荐