Android如何美化底部菜单栏-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
10年积累的网站制作、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有德安免费网站建设让你可以放心的选择与我们合作。这篇文章主要讲解了Android如何美化底部菜单栏,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
众所周知,android的底部菜单栏太重要,平时项目一般都是需要用到的,但是网上关于这方面的demo做得太丑了,实在惨不忍睹,所以这里便用RadioGroup+Fragment的方式写了一个,顺便美化了一下,需要的可以看下。
效果图:
项目结构
MainActivity.java
public class MainActivity extends AppCompatActivity { private FrameLayout frameLayout; private RadioGroup radioGroup; private Fragment[] mFragments; private int mIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFragment(); setRadioGroupListener(); } private void initFragment() { radioGroup = (RadioGroup) findViewById(R.id.radioGroup); frameLayout = (FrameLayout) findViewById(R.id.fl_content); HomeFragment homeFragment = new HomeFragment(); ShopFragment shopFragment = new ShopFragment(); LiveFragment liveFragment = new LiveFragment(); ShoppingCarFragment shoppingCarFragment = new ShoppingCarFragment(); MineFragment mineFragment = new MineFragment(); //添加到数组 mFragments = new Fragment[]{homeFragment, shopFragment, liveFragment, shoppingCarFragment, mineFragment}; //开启事务 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); //添加首页 ft.add(R.id.fl_content, homeFragment).commit(); //默认设置为第0个 setIndexSelected(0); } private void setIndexSelected(int index) { if (mIndex == index) { return; } FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); //隐藏 ft.hide(mFragments[mIndex]); //判断是否添加 if (!mFragments[index].isAdded()) { ft.add(R.id.fl_content, mFragments[index]).show(mFragments[index]); } else { ft.show(mFragments[index]); } ft.commit(); //再次赋值 mIndex = index; } private void setRadioGroupListener() { radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rb_home: setIndexSelected(0); break; case R.id.rb_shop: setIndexSelected(1); break; case R.id.rb_live: setIndexSelected(2); break; case R.id.rb_shopping_car: setIndexSelected(3); break; case R.id.rb_mine: setIndexSelected(4); break; default: setIndexSelected(0); break; } } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { //仅当activity为task根(即首个启动activity)时才生效,这个方法不会改变task中的activity状态, // 按下返回键的作用跟按下HOME效果一样;重新点击应用还是回到应用退出前的状态; moveTaskToBack(false); return true; } return super.onKeyDown(keyCode, event); } }
标题名称:Android如何美化底部菜单栏-创新互联
转载来于:http://myzitong.com/article/ecced.html