我想要在三个页面实现不同的东西,并且通过bottomNavigationBar进行切换
1、在build.gradle中引入bottom-navigation-bar
dependencies中加入
compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.1'
2、创建三个fragmentActivity
其中之一
public class FragActivity_Me extends Fragment { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_3, container, false); return view; } }
3、在activity_maiin.xml中插入fragment
<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=".TabBarActivity"> <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>
4、修改其中一个FragmentActivity
public class FragActivity_Look extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
btn = view.findViewById(R.id.bt_set);
return view;
}
}
5、修改MainActivity
首先public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener
申明变量
private BottomNavigationBar bottomNavigationBar; private FrameLayout frameLayout; private FragmentTransaction transaction; private FragActivity_Look fragActivity_look; private FragActivity_Map fragActivity_map; private FragActivity_Me fragActivity_me; private Fragment mFragment;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout = (FrameLayout) findViewById(R.id.fragment_content); bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); bottomNavigationBar.setActiveColor(R.color.colorAccent)//设置Item选中颜色方法 .setInActiveColor(R.color.colorPrimary)//设置Item未选中颜色方法 .setBarBackgroundColor("#FFFFFF");//背景颜色 bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED); bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC); bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look"). setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map"). setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me"). setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise(); bottomNavigationBar.setTabSelectedListener(this); //initFragment(); fragActivity_look=new FragActivity_Look(); fragActivity_map = new FragActivity_Map(); fragActivity_me = new FragActivity_Me(); transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment_content, fragActivity_map).commit(); mFragment = fragActivity_map; }
切换fragment
private void switchFragment(Fragment fragment) { //判断当前显示的Fragment是不是切换的Fragment if(mFragment != fragment) { //判断切换的Fragment是否已经添加过 if (!fragment.isAdded()) { //如果没有,则先把当前的Fragment隐藏,把切换的Fragment添加上 getSupportFragmentManager().beginTransaction().hide(mFragment) .add(R.id.fragment_content,fragment).commit(); } else { //如果已经添加过,则先把当前的Fragment隐藏,把切换的Fragment显示出来 getSupportFragmentManager().beginTransaction().hide(mFragment).show(fragment).commit(); } mFragment = fragment; } }
实现OnTabSelectedListener接口所需要的方法
@Override public void onTabSelected(int position) { switch (position){ case 0: switchFragment(fragActivity_look); break; case 1: switchFragment(fragActivity_map); break; case 2: switchFragment(fragActivity_me); break; } } @Override public void onTabUnselected(int position) { } @Override public void onTabReselected(int position) { }
还需要三个小图片(24像素左右)及定义colors即可
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.fragment_content);
bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar.setActiveColor(R.color.colorAccent)//设置Item选中颜色方法
.setInActiveColor(R.color.colorPrimary)//设置Item未选中颜色方法
.setBarBackgroundColor("#FFFFFF");//背景颜色
bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.bt_look, "Look").
setActiveColorResource(R.color.teal)) .addItem(new BottomNavigationItem(R.mipmap.bt_map, "Map").
setActiveColorResource(R.color.blue)) .addItem(new BottomNavigationItem(R.mipmap.bt_user, "Me").
setActiveColorResource(R.color.grey)) .setFirstSelectedPosition(1) .initialise();
bottomNavigationBar.setTabSelectedListener(this);
//initFragment();
fragActivity_look=new FragActivity_Look();
fragActivity_map = new FragActivity_Map();
fragActivity_me = new FragActivity_Me();
transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_content, fragActivity_map).commit();
mFragment = fragActivity_map;
}