Android使用RadioGroup实现底部导航栏

RadioGroup实现底部导航栏效果,如图::

成都创新互联主营寻乌网站建设的网络公司,主营网站建设方案,成都App定制开发,寻乌h5小程序开发搭建,寻乌网站营销推广欢迎寻乌等地区企业咨询

 Android使用RadioGroup实现底部导航栏

实现可最基本的导航栏功能,不能左右滑动,只能点击

1.内嵌的fragment的布局:

<?xml version="1.0" encoding="utf-8"?>

 

2.fragment的activity代码:

public class FrHome extends Fragment {

 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home, container, false);
 return view;
 }

}

以此为例根据需要编写不同的fragment布局等等。

3.装载fragment的界面布局如下(其中使用了selector进行实现点击改变图标和文字颜色):

点击改变文字颜色:

<?xml version="1.0" encoding="utf-8"?>

 
 

点击改变图标:

<?xml version="1.0" encoding="utf-8"?>

 
 

界面布局:

<?xml version="1.0" encoding="utf-8"?>


 

 
 
  
  
  
 
 


4.装载fragment的界面的activity代码(加入双击返回键则退出应用):

public class ActHome extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

 @BindView(R.id.frame_layout)
 FrameLayout frameLayout;
 @BindView(R.id.tab_home)
 RadioButton tabHome;
 @BindView(R.id.tab_health)
 RadioButton tabHealth;
 @BindView(R.id.tab_personal)
 RadioButton tabPersonal;
 @BindView(R.id.tab_bar)
 RadioGroup tabBar;

 public final static String ACTION_EXIT_SYSTEM = "sys_exit";

 private FragmentManager manager;
 private FragmentTransaction transaction;
 private FrHome frHome;
 private FrHealth frHealth;
 private FrPersonal frPersonal;
 private long mExitTime;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_home);
 ButterKnife.bind(this);
 RadioButton tabHome = (RadioButton) tabBar.getChildAt(0);
 tabHome.setChecked(true);
 tabBar.setOnCheckedChangeListener(this);
 initFragment();
 }

 private void initFragment() {
 manager = getSupportFragmentManager();
 transaction = manager.beginTransaction();
 frHome = new FrHome();
 transaction.add(R.id.frame_layout,frHome);
 transaction.commit();
 }

 @Override
 public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
 switch (checkedId) {
  case R.id.tab_home:
  FragmentTransaction ft1 = manager.beginTransaction();
  hideAll(ft1);
  if (frHome!=null){
   ft1.show(frHome);
  }else {
   frHome=new FrHome();
   ft1.add(R.id.frame_layout,frHome);
  }
  ft1.commit();
  break;
  case R.id.tab_health:
  FragmentTransaction ft2 = manager.beginTransaction();
  hideAll(ft2);
  if (frHealth!=null){
   ft2.show(frHealth);
  }else {
   frHealth = new FrHealth();
   ft2.add(R.id.frame_layout,frHealth);
  }
  ft2.commit();
  break;
  case R.id.tab_personal:
  FragmentTransaction ft5 = manager.beginTransaction();
  hideAll(ft5);
  if (frPersonal!=null){
   ft5.show(frPersonal);
  }else {
   frPersonal = new FrPersonal();
   ft5.add(R.id.frame_layout, frPersonal);
  }
  ft5.commit();
  break;
 }
 }

 private void hideAll(FragmentTransaction ft){
 if (ft==null){
  return;
 }
 if (frHome!=null){
  ft.hide(frHome);
 }
 if (frHealth!=null){
  ft.hide(frHealth);
 }
 if (frPersonal!=null){
  ft.hide(frPersonal);
 }
 }

 @Override
 public void onBackPressed() {
 if ((System.currentTimeMillis() - mExitTime) > 2000) {
  Toast.makeText(ActHome.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
  mExitTime = System.currentTimeMillis();
 } else {
  new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
   onExit(ActHome.this);
  }
  }, 500);
 }
 }

 public static void onExit(final Context context) {
 try {
  Intent intent = new Intent();
  intent.setAction(context.getApplicationContext().getPackageName() + ACTION_EXIT_SYSTEM);
  context.sendBroadcast(intent);
  // MobclickAgent.onKillProcess(context);
  new Handler().postDelayed(new Runnable() {
  @Override
  public void run() {
   System.exit(0);
  }
  }, 200);
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


标题名称:Android使用RadioGroup实现底部导航栏
网站URL:http://bzwzjz.com/article/jdihjd.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站制作 成都网站建设公司 网站制作 成都网站建设 盐亭网站设计 网站建设 重庆网站设计 企业网站建设公司 网站建设方案 成都网站建设 成都网站建设 网站制作报价 移动网站建设 成都定制网站建设 自适应网站建设 网站建设方案 成都网站设计 成都网站制作 网站建设 成都响应式网站建设 成都网站设计 成都网站建设