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) { }}
阅读全文
0 0
- bottomNavigationbar
- BottomNavigationBar
- BottomNavigationBar
- BottomNavigationBar
- BottomNavigationBar
- BottomNavigationBar使用
- TabHost & BottomNavigationBar
- Android BottomNavigationBar的使用
- BottomNavigationBar的使用
- BottomNavigationBar跳坑小记
- 底部导航---BottomNavigationBar
- 底部导航之BottomNavigationBar
- android--导航(BottomNavigationBar)
- BottomNavigationBar简单使用
- 导航栏BottomNavigationBar使用
- BottomNavigationBar的坑
- BottomNavigationBar的使用
- 导航栏 BottomNavigationBar 详解
- C#执行带参数的mysql语句
- Android 自动更新Apk组件封装
- VS无法启动程序,系统找不到指定的资源
- 开发小技巧-商品价格中间划价实现
- seo必须知道的6大点
- BottomNavigationBar
- 命名空间的使用
- AJAX异步校验用户名
- Java动态代理的实现方法
- Cocos2d-x之LUA脚本引擎浅析
- js实现倒计时60秒的简单代码
- #define中#和##的作用
- 分割候选区域--FastMask: Segment Multi-scale Object Candidates in One Shot
- nginx的使用