//依赖
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(); } }); } }