底部導航切換
阿新 • • 發佈:2018-11-09
一:匯入底部導航依賴
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 前面寫