1. 程式人生 > >使用BottomTabBar實現底部導航頁(頁面切換)

使用BottomTabBar實現底部導航頁(頁面切換)

開始使用之前先匯入

implementation 'com.hjm:BottomTabBar:1.2.2'

主頁面佈局檔案

<?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"
    xmlns:hjm="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <com.hjm.bottomtabbar.BottomTabBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        hjm:tab_bar_background="#FFFFFF"
        hjm:tab_divider_background="#373737"
        hjm:tab_font_size="28px"
        hjm:tab_img_font_padding="6dp"
        hjm:tab_img_height="50px"
        hjm:tab_img_width="50px"
        hjm:tab_isshow_divider="true"
        hjm:tab_padding_bottom="4px"
        hjm:tab_padding_top="10px"
        hjm:tab_selected_color="#2784E7"
        hjm:tab_unselected_color="#282828">

    </com.hjm.bottomtabbar.BottomTabBar>

</LinearLayout>

//主頁面activity

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bottom_bar)
    BottomTabBar bottomTabbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        bottomTabbar.init(getSupportFragmentManager(), 750, 1334)
//                .setImgSize(50, 50)
//                .setFontSize(28)
//                .setTabPadding(10, 6, 4)
//                .setChangeColor(Color.parseColor("#2784E7"),Color.parseColor("#282828"))
                .addTabItem("收藏", R.mipmap.ic_launcher, R.mipmap.ic_launcher, ScFragment.class)
                .addTabItem("段子", R.mipmap.ic_launcher, R.mipmap.ic_launcher, DZFragment.class)
                .addTabItem("發現", R.mipmap.ic_launcher, R.mipmap.ic_launcher, FXFragment.class)
                .addTabItem("視訊", R.mipmap.ic_launcher, R.mipmap.ic_launcher, SPFragment.class)
//                .isShowDivider(true)
//                .setDividerColor(Color.parseColor("#373737"))
//                .setTabBarBackgroundColor(Color.parseColor("#FFFFFF"))
                .setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
                    @Override
                    public void onTabChange(int position, String name, View view) {
                        if (position == 1)
                            bottomTabbar.setSpot(1, false);
                    }
                })
                .setSpot(1, true)
                .setSpot(2, true);
    }
    public void setShowTabBar(boolean isShow){
        if (isShow){
            bottomTabbar.getTabBar().setVisibility(View.VISIBLE);
        }else {
            bottomTabbar.getTabBar().setVisibility(View.GONE);
        }
    }
}