BottomNavigationBar

来源:互联网 发布:高中毕业学java 编辑:程序博客网 时间:2024/06/09 20:59

使用Gradle构建:

compile 'com.ashokvarma.android:bottom-navigation-bar:0.9.5'
界面布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    //用于界面布局改变    <FrameLayout android:id="@+id/fragment_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1">    </FrameLayout>    <com.ashokvarma.bottomnavigation.BottomNavigationBar        android:id="@+id/bottom_navigation_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom" /></LinearLayout>

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {    BottomNavigationBar bar;    private FragmentOne fragmentOne;    private FragmentTwo fragmentTwo;    private FragmentThree fragmentThree;    private FragmentFore fragmentFore;    private FragmentFive fragmentFive;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bar=(BottomNavigationBar)  findViewById(R.id.bottom_navigation_bar);        bar.setMode(BottomNavigationBar.MODE_DEFAULT);//setActiveColorResource(R.color.colorAccent));        bar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);        bar.addItem(new BottomNavigationItem(R.mipmap.ok, "首页").setActiveColor(R.color.colorAccent));        bar.addItem(new BottomNavigationItem(R.mipmap.okn, "找车").setActiveColor(R.color.colorAccent));        bar.addItem(new BottomNavigationItem(R.mipmap.ok, "优惠").setActiveColor(R.color.colorAccent));        bar.addItem(new BottomNavigationItem(R.mipmap.okn, "论坛").setActiveColor(R.color.colorAccent));        bar.addItem(new BottomNavigationItem(R.mipmap.ok, "我的").setActiveColor(R.color.colorAccent));        bar.initialise();        bar.setTabSelectedListener(this);        setDefaultFragment();// bar.setFirstSelectedPosition(0);设置默认选项    }    private void setDefaultFragment() {        FragmentManager fragmentManager=getSupportFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        hideFragments(fragmentTransaction);        fragmentOne=new FragmentOne();        fragmentTransaction.add(R.id.fragment_content,fragmentOne);        fragmentTransaction.commit();    }    /**     * 隐藏fragment     * @param transaction     */    private void hideFragments(FragmentTransaction transaction) {        if (fragmentOne != null) {            transaction.hide(fragmentOne);        }        if (fragmentTwo != null) {            transaction.hide(fragmentTwo);        }        if (fragmentThree != null) {            transaction.hide(fragmentThree);        }        if (fragmentFore != null) {            transaction.hide(fragmentFore);        }        if (fragmentFive != null) {            transaction.hide(fragmentFive);        }    }    @Override    public void onTabSelected(int position) {        FragmentManager fragmentManager=getSupportFragmentManager();        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();        hideFragments(fragmentTransaction);        switch (position){            case 0:                if (fragmentOne==null){                    fragmentOne=new FragmentOne();                    fragmentTransaction.add(R.id.fragment_content,fragmentOne);                }else {                    fragmentTransaction.show(fragmentOne);                }                break;            case 1:                if (fragmentTwo==null){                    fragmentTwo=new FragmentTwo();                    fragmentTransaction.add(R.id.fragment_content,fragmentTwo);                }else {                    fragmentTransaction.show(fragmentTwo);                }                break;            case 2:                if (fragmentThree==null){                    fragmentThree=new FragmentThree();                    fragmentTransaction.add(R.id.fragment_content,fragmentThree);                }else {                    fragmentTransaction.show(fragmentThree);                }                break;            case 3:                if (fragmentFore==null){                    fragmentFore=new FragmentFore();                    fragmentTransaction.add(R.id.fragment_content,fragmentFore);                }else {                    fragmentTransaction.show(fragmentFore);                }                break;            case 4:                if (fragmentFive==null){                    fragmentFive=new FragmentFive();                    fragmentTransaction.add(R.id.fragment_content,fragmentFive);                }else {                    fragmentTransaction.show(fragmentFive);                }                break;        }        fragmentTransaction.commit();    }    @Override    public void onTabUnselected(int position) {    }    @Override    public void onTabReselected(int position) {    }}


原创粉丝点击