android开发中如何实现底部导航

android开发中如何实现底部导航

小编给大家分享一下 android开发中如何实现底部导航,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

使用LinearLayout 底部布局+p_w_picpathView 实现

底部四个主导航页面 布局文件 activity_main.xml

android开发中如何实现底部导航

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.tastelibrary.MainActivity"tools:ignore="MergeRootFrame"><FrameLayoutandroid:id="@+id/fl_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></FrameLayout><LinearLayoutandroid:id="@+id/ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:id="@+id/ll_recipe"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/p_w_picpath_recipe"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/recipe_btn_selector"/></LinearLayout><LinearLayoutandroid:id="@+id/ll_kitchen"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/p_w_picpath_kitchen"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/kitchen_btn_selector"/></LinearLayout><LinearLayoutandroid:id="@+id/ll_find"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/p_w_picpath_find"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/find_btn_selector"/></LinearLayout><LinearLayoutandroid:id="@+id/ll_user"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical"><ImageViewandroid:id="@+id/p_w_picpath_user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/user_btn_selector"/></LinearLayout></LinearLayout></LinearLayout>MainActivitypackagecom.example.tastelibrary;importandroid.support.v7.app.ActionBarActivity;importandroid.support.v7.app.ActionBar;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentManager;importandroid.support.v4.app.FragmentTransaction;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.view.ViewGroup;importandroid.widget.ImageView;importandroid.widget.LinearLayout;importandroid.os.Build;publicclassMainActivityextendsActionBarActivityimplementsOnClickListener{privateLinearLayoutll_recipe;privateLinearLayoutll_kitchen;privateLinearLayoutll_find;privateLinearLayoutll_user;privateImageViewp_w_picpath_home;privateImageViewp_w_picpath_friends;privateImageViewp_w_picpath_message;privateImageViewp_w_picpath_more;//Fragment管理器privateFragmentManagerfm=this.getSupportFragmentManager();privateFragmentTransactionft;privateRecipeFragmentfragmentPage1;privateFindFragmentfragmentPage2;privateKitchenFragmentfragmentPage3;privateUserFragmentfragmentPage4;ActionBarmyActionBar;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myActionBar=getSupportActionBar();initView();//开始事务(每次改变Fragment管理器之后都要提交)ft=fm.beginTransaction();home();//提交事务ft.commit();}privatevoidinitView(){ll_recipe=(LinearLayout)findViewById(R.id.ll_recipe);ll_kitchen=(LinearLayout)findViewById(R.id.ll_kitchen);ll_find=(LinearLayout)findViewById(R.id.ll_find);ll_user=(LinearLayout)findViewById(R.id.ll_user);p_w_picpath_home=(ImageView)findViewById(R.id.p_w_picpath_recipe);p_w_picpath_friends=(ImageView)findViewById(R.id.p_w_picpath_kitchen);p_w_picpath_message=(ImageView)findViewById(R.id.p_w_picpath_find);p_w_picpath_more=(ImageView)findViewById(R.id.p_w_picpath_user);ll_recipe.setOnClickListener(this);ll_kitchen.setOnClickListener(this);ll_find.setOnClickListener(this);ll_user.setOnClickListener(this);ll_recipe.setSelected(true);p_w_picpath_home.setSelected(true);}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}@OverridepublicvoidonClick(Viewv){//每次点击时都需要重新开始事务ft=fm.beginTransaction();//把显示的Fragment隐藏setSelected();switch(v.getId()){caseR.id.ll_recipe:ll_recipe.setSelected(true);p_w_picpath_home.setSelected(true);home();break;caseR.id.ll_kitchen:ll_kitchen.setSelected(true);p_w_picpath_friends.setSelected(true);friend();break;caseR.id.ll_find:ll_find.setSelected(true);p_w_picpath_message.setSelected(true);message();break;caseR.id.ll_user:ll_user.setSelected(true);p_w_picpath_more.setSelected(true);more();break;}ft.commit();}/***设置每个按钮是否被选中*/privatevoidsetSelected(){ll_recipe.setSelected(false);ll_kitchen.setSelected(false);ll_find.setSelected(false);ll_user.setSelected(false);p_w_picpath_home.setSelected(false);p_w_picpath_friends.setSelected(false);p_w_picpath_message.setSelected(false);p_w_picpath_more.setSelected(false);if(fragmentPage1!=null){//隐藏Fragmentft.hide(fragmentPage1);}if(fragmentPage2!=null){ft.hide(fragmentPage2);}if(fragmentPage3!=null){ft.hide(fragmentPage3);}if(fragmentPage4!=null){ft.hide(fragmentPage4);}}privatevoidhome(){if(fragmentPage1==null){fragmentPage1=newRecipeFragment();/*添加到Fragment管理器中这里如果用replace,当每次调用时都会把前一个Fragment给干掉,这样就导致了每一次都要创建、销毁,数据就很难保存,用add就不存在这样的问题了,当Fragment存在时候就让它显示,不存在时就创建,这样的话数据就不需要自己保存了,因为第一次创建的时候就已经保存了,只要不销毁一直都将存在*/ft.add(R.id.fl_content,fragmentPage1);}else{//显示Fragmentft.show(fragmentPage1);}}privatevoidfriend(){if(fragmentPage2==null){fragmentPage2=newFindFragment();ft.add(R.id.fl_content,fragmentPage2);}else{ft.show(fragmentPage2);}}privatevoidmessage(){if(fragmentPage3==null){fragmentPage3=newKitchenFragment();ft.add(R.id.fl_content,fragmentPage3);}else{ft.show(fragmentPage3);}}privatevoidmore(){if(fragmentPage4==null){fragmentPage4=newUserFragment();ft.add(R.id.fl_content,fragmentPage4);}else{ft.show(fragmentPage4);}}}

以上是“ android开发中如何实现底部导航”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2022-01-17 22:01:36
收藏
分享
海报
0 条评论
38
上一篇:Android开发中如何实现数据存储 下一篇:如何使用Animation作为ImageView的背景
目录

    推荐阅读

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码