Android实习札记(5)---Fragment之底部导航栏的实现

这篇博客介绍了如何在Android应用中使用Fragment来实现底部导航栏功能。通过展示实现流程图和详细代码,作者逐步讲解了如何创建布局、初始化组件、处理点击事件以及切换Fragment,帮助读者理解这一常见UI设计的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android实习札记(5)---Fragment之底部导航栏的实现

——转载请注明出处:coder-pig



在Part 4我们回顾了一下Fragment的基本概念,在本节中我们就来学习Fragment应用的简单例子吧!

就是使用Fragment来实现简单的底部导航栏,先贴下效果图:



看上去很简单,实现起来也是很简单的哈!那么接着下来就看下实现的流程图吧:


实现流程图:




看完流程图是不是有大概的思路了,那么接着就开始代码的编写吧:


代码实现:

①先写布局,布局的话很简单,一个FrameLayout用来放Fragment,底部一个大的LinearLayout

放着三个小Item,每个Item的布局如下:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout    
  2.             android:id="@+id/setting_layout"    
  3.             android:layout_width="0dp"    
  4.             android:layout_height="match_parent"    
  5.             android:layout_weight="1" >    
  6.     
  7.             <LinearLayout    
  8.                 android:layout_width="match_parent"    
  9.                 android:layout_height="wrap_content"    
  10.                 android:layout_centerVertical="true"    
  11.                 android:orientation="vertical" >    
  12.     
  13.                 <ImageView    
  14.                     android:id="@+id/setting_image"    
  15.                     android:layout_width="wrap_content"    
  16.                     android:layout_height="wrap_content"    
  17.                     android:layout_gravity="center_horizontal"    
  18.                     android:src="@drawable/ic_tabbar_settings_normal" />    
  19.     
  20.                 <TextView    
  21.                     android:id="@+id/setting_text"    
  22.                     android:layout_width="wrap_content"    
  23.                     android:layout_height="wrap_content"    
  24.                     android:layout_gravity="center_horizontal"    
  25.                     android:text="设置"    
  26.                     android:textColor="#7597B3" />    
  27.             </LinearLayout>    
  28.         </RelativeLayout>    

copy多两个,改下图片,文本资源就可以了,完整布局代码如下:

activity_main.xml

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="match_parent"    
  3.     android:layout_height="match_parent"    
  4.     android:orientation="vertical" >    
  5.       
  6.     <FrameLayout    
  7.         android:id="@+id/content"    
  8.         android:layout_width="match_parent"    
  9.         android:layout_height="0dp"    
  10.         android:layout_weight="1" >    
  11.     </FrameLayout>    
  12.       
  13.     <LinearLayout    
  14.         android:layout_width="match_parent"    
  15.         android:layout_height="60dp"    
  16.         android:background="#FFFFFF" >    
  17.     
  18.         <RelativeLayout    
  19.             android:id="@+id/course_layout"    
  20.             android:layout_width="0dp"    
  21.             android:layout_height="match_parent"    
  22.             android:layout_weight="1" >    
  23.     
  24.             <LinearLayout    
  25.                 android:layout_width="match_parent"    
  26.                 android:layout_height="wrap_content"    
  27.                 android:layout_centerVertical="true"    
  28.                 android:orientation="vertical" >    
  29.     
  30.                 <ImageView    
  31.                     android:id="@+id/course_image"    
  32.                     android:layout_width="wrap_content"    
  33.                     android:layout_height="wrap_content"    
  34.                     android:layout_gravity="center_horizontal"    
  35.                     android:src="@drawable/ic_tabbar_course_normal" />    
  36.     
  37.                 <TextView    
  38.                     android:id="@+id/course_text"    
  39.                     android:layout_width="wrap_content"    
  40.                     android:layout_height="wrap_content"    
  41.                     android:layout_gravity="center_horizontal"    
  42.                     android:text="日程"    
  43.                     android:textColor="#7597B3" />    
  44.             </LinearLayout>    
  45.         </RelativeLayout>    
  46.           
  47.         <RelativeLayout    
  48.             android:id="@+id/found_layout"    
  49.             android:layout_width="0dp"    
  50.             android:layout_height="match_parent"    
  51.             android:layout_weight="1" >    
  52.     
  53.             <LinearLayout    
  54.                 android:layout_width="match_parent"    
  55.                 android:layout_height="wrap_content"    
  56.                 android:layout_centerVertical="true"    
  57.                 android:orientation="vertical" >    
  58.     
  59.                 <ImageView    
  60.                     android:id="@+id/found_image"    
  61.                     android:layout_width="wrap_content"    
  62.                     android:layout_height="wrap_content"    
  63.                     android:layout_gravity="center_horizontal"    
  64.                     android:src="@drawable/ic_tabbar_found_normal" />    
  65.     
  66.                 <TextView    
  67.                     android:id="@+id/found_text"    
  68.                     android:layout_width="wrap_content"    
  69.                     android:layout_height="wrap_content"    
  70.                     android:layout_gravity="center_horizontal"    
  71.                     android:text="发现"    
  72.                     android:textColor="#7597B3" />    
  73.             </LinearLayout>    
  74.         </RelativeLayout>    
  75.           
  76.           
  77.         <RelativeLayout    
  78.             android:id="@+id/setting_layout"    
  79.             android:layout_width="0dp"    
  80.             android:layout_height="match_parent"    
  81.             android:layout_weight="1" >    
  82.     
  83.             <LinearLayout    
  84.                 android:layout_width="match_parent"    
  85.                 android:layout_height="wrap_content"    
  86.                 android:layout_centerVertical="true"    
  87.                 android:orientation="vertical" >    
  88.     
  89.                 <ImageView    
  90.                     android:id="@+id/setting_image"    
  91.                     android:layout_width="wrap_content"    
  92.                     android:layout_height="wrap_content"    
  93.                     android:layout_gravity="center_horizontal"    
  94.                     android:src="@drawable/ic_tabbar_settings_normal" />    
  95.     
  96.                 <TextView    
  97.                     android:id="@+id/setting_text"    
  98.                     android:layout_width="wrap_content"    
  99.                     android:layout_height="wrap_content"    
  100.                     android:layout_gravity="center_horizontal"    
  101.                     android:text="设置"    
  102.                     android:textColor="#7597B3" />    
  103.             </LinearLayout>    
  104.         </RelativeLayout>    
  105.                   
  106.           
  107.   </LinearLayout>  
  108. </LinearLayout>    

②接着就写三个Fragment的布局,这个看你需要了,这里就一个TextView

写完一式三份,复制多两个,改下颜色和文字

fg1.xml

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:background="#FAECD8"  
  7.     android:orientation="vertical" >  
  8.       
  9.     <TextView   
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="日程表Fragment"  
  13.     />  
  14.   
  15. </LinearLayout>  

③接着写三个Fragment的实现类,同样一式三份,改下inflate加载的Fragment即可

Fragment1.java

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.fragmentforhost;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class Fragment1 extends Fragment {  
  10.     @Override  
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  12.             Bundle savedInstanceState) {  
  13.         View view = inflater.inflate(R.layout.fg1, container,false);  
  14.         return view;  
  15.     }  
  16. }  

④接着就到MainActivity的编写了,也很简单,自己看看吧,就不多解释了

就定义的几个方法,初始化选项,选中处理,以及隐藏所有Fragment的方法!

MainActivity.java

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.fragmentforhost;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.app.FragmentManager;  
  6. import android.support.v4.app.FragmentTransaction;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.FrameLayout;  
  10. import android.widget.ImageView;  
  11. import android.widget.RelativeLayout;  
  12. import android.widget.TextView;  
  13.   
  14.   
  15.   
  16. public class MainActivity extends FragmentActivity implements OnClickListener{  
  17.   
  18.     //定义3个Fragment的对象  
  19.     private Fragment1 fg1;  
  20.     private Fragment2 fg2;  
  21.     private Fragment3 fg3;  
  22.     //帧布局对象,就是用来存放Fragment的容器  
  23.     private FrameLayout flayout;  
  24.     //定义底部导航栏的三个布局  
  25.     private RelativeLayout course_layout;  
  26.     private RelativeLayout found_layout;  
  27.     private RelativeLayout settings_layout;  
  28.     //定义底部导航栏中的ImageView与TextView  
  29.     private ImageView course_image;  
  30.     private ImageView found_image;  
  31.     private ImageView settings_image;  
  32.     private TextView course_text;  
  33.     private TextView settings_text;  
  34.     private TextView found_text;  
  35.     //定义要用的颜色值  
  36.     private int whirt = 0xFFFFFFFF;  
  37.     private int gray = 0xFF7597B3;  
  38.     private int blue =0xFF0AB2FB;  
  39.     //定义FragmentManager对象  
  40.     FragmentManager fManager;  
  41.       
  42.       
  43.     @Override  
  44.     protected void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.activity_main);  
  47.         fManager = getSupportFragmentManager();  
  48.         initViews();  
  49.     }  
  50.       
  51.       
  52.     //完成组件的初始化  
  53.     public void initViews()  
  54.     {  
  55.         course_image = (ImageView) findViewById(R.id.course_image);  
  56.         found_image = (ImageView) findViewById(R.id.found_image);  
  57.         settings_image = (ImageView) findViewById(R.id.setting_image);  
  58.         course_text = (TextView) findViewById(R.id.course_text);  
  59.         found_text = (TextView) findViewById(R.id.found_text);  
  60.         settings_text = (TextView) findViewById(R.id.setting_text);  
  61.         course_layout = (RelativeLayout) findViewById(R.id.course_layout);  
  62.         found_layout = (RelativeLayout) findViewById(R.id.found_layout);  
  63.         settings_layout = (RelativeLayout) findViewById(R.id.setting_layout);  
  64.         course_layout.setOnClickListener(this);  
  65.         found_layout.setOnClickListener(this);   
  66.         settings_layout.setOnClickListener(this);  
  67.     }  
  68.       
  69.     //重写onClick事件  
  70.     @Override  
  71.     public void onClick(View view) {  
  72.         switch (view.getId()) {  
  73.         case R.id.course_layout:  
  74.             setChioceItem(0);  
  75.             break;  
  76.         case R.id.found_layout:  
  77.             setChioceItem(1);  
  78.             break;  
  79.         case R.id.setting_layout:  
  80.             setChioceItem(2);  
  81.             break;  
  82.         default:  
  83.             break;  
  84.         }  
  85.           
  86.     }  
  87.       
  88.       
  89.     //定义一个选中一个item后的处理  
  90.     public void setChioceItem(int index)  
  91.     {  
  92.         //重置选项+隐藏所有Fragment  
  93.         FragmentTransaction transaction = fManager.beginTransaction();    
  94.         clearChioce();  
  95.         hideFragments(transaction);  
  96.         switch (index) {  
  97.         case 0:  
  98.             course_image.setImageResource(R.drawable.ic_tabbar_course_pressed);    
  99.             course_text.setTextColor(blue);  
  100.             course_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);  
  101.             if (fg1 == null) {    
  102.                 // 如果fg1为空,则创建一个并添加到界面上    
  103.                 fg1 = new Fragment1();    
  104.                 transaction.add(R.id.content, fg1);    
  105.             } else {    
  106.                 // 如果MessageFragment不为空,则直接将它显示出来    
  107.                 transaction.show(fg1);    
  108.             }    
  109.             break;    
  110.   
  111.         case 1:  
  112.             found_image.setImageResource(R.drawable.ic_tabbar_found_pressed);    
  113.             found_text.setTextColor(blue);  
  114.             found_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);  
  115.             if (fg2 == null) {    
  116.                 // 如果fg1为空,则创建一个并添加到界面上    
  117.                 fg2 = new Fragment2();    
  118.                 transaction.add(R.id.content, fg2);    
  119.             } else {    
  120.                 // 如果MessageFragment不为空,则直接将它显示出来    
  121.                 transaction.show(fg2);    
  122.             }    
  123.             break;        
  124.           
  125.          case 2:  
  126.             settings_image.setImageResource(R.drawable.ic_tabbar_settings_pressed);    
  127.             settings_text.setTextColor(blue);  
  128.             settings_layout.setBackgroundResource(R.drawable.ic_tabbar_bg_click);  
  129.             if (fg3 == null) {    
  130.                 // 如果fg1为空,则创建一个并添加到界面上    
  131.                 fg3 = new Fragment3();    
  132.                 transaction.add(R.id.content, fg3);    
  133.             } else {    
  134.                 // 如果MessageFragment不为空,则直接将它显示出来    
  135.                 transaction.show(fg3);    
  136.             }    
  137.             break;                   
  138.         }  
  139.         transaction.commit();  
  140.     }  
  141.       
  142.     //隐藏所有的Fragment,避免fragment混乱  
  143.     private void hideFragments(FragmentTransaction transaction) {    
  144.         if (fg1 != null) {    
  145.             transaction.hide(fg1);    
  146.         }    
  147.         if (fg2 != null) {    
  148.             transaction.hide(fg2);    
  149.         }    
  150.         if (fg3 != null) {    
  151.             transaction.hide(fg3);    
  152.         }    
  153.     }    
  154.           
  155.       
  156.     //定义一个重置所有选项的方法  
  157.     public void clearChioce()  
  158.     {  
  159.         course_image.setImageResource(R.drawable.ic_tabbar_course_normal);  
  160.         course_layout.setBackgroundColor(whirt);  
  161.         course_text.setTextColor(gray);  
  162.         found_image.setImageResource(R.drawable.ic_tabbar_found_normal);  
  163.         found_layout.setBackgroundColor(whirt);  
  164.         found_text.setTextColor(gray);  
  165.         settings_image.setImageResource(R.drawable.ic_tabbar_settings_normal);  
  166.         settings_layout.setBackgroundColor(whirt);  
  167.         settings_text.setTextColor(gray);  
  168.     }  
  169.   
  170. }  



最后说几句:

代码就上面的一点点,解析也很详细,看多两遍就应该能看懂了,

另外注意一点就是Fragment相关类导入的时候是v4包还是app包!

那里很容易出错的,关于app与v4包的Fragment可以看札记(3)的解析!

恩,这节就写到这里,下一节会实现Fragment与ViewPager的简单应用!

敬请期待!



本节代码下载:

http://pan.baidu.com/s/1gdel98B

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值