Fragment+FragmentTabHost实现仿新浪微博底部菜单栏

  在上一篇文章中,我们花了大量的篇幅来讲解Fragment这个新引进类的使用,目的就是为了让大家能够牢牢的掌握它的使用方法,以便读者在今后的开发中能够熟练的使用它。

一、实现效果图

20130603204331671.jpg


20130603204337781.jpg


二、项目工程结构

20130603205005046.jpg

20130603205011437.jpg

三、详细代码编写


1、主tab布局界面,main_tab_layout:

  1. <font color="#000"><font face="Arial"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:orientation="vertical" >

  6.     <FrameLayout
  7.         android:id="@+id/realtabcontent"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="0dip"
  10.         android:layout_weight="1" />

  11.     <android.support.v4.app.FragmentTabHost
  12.         android:id="@android:id/tabhost"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="wrap_content" 
  15.         android:background="@drawable/maintab_toolbar_bg">

  16.         <FrameLayout
  17.             android:id="@android:id/tabcontent"
  18.             android:layout_width="0dp"
  19.             android:layout_height="0dp"
  20.             android:layout_weight="0" />            
  21.     </android.support.v4.app.FragmentTabHost>

  22. </LinearLayout></font></font>
复制代码
2、Tab按钮选项布局,tab_item_view.xml:

  1. <font color="#000"><font face="Arial"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:gravity="center"
  6.     android:orientation="vertical" >

  7.     <ImageView
  8.         android:id="@+id/imageview"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:focusable="false"
  12.         android:padding="3dp" 
  13.         android:src="@drawable/tab_home_btn">
  14.     </ImageView>

  15.     <TextView
  16.         android:id="@+id/textview"       
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content" 
  19.         android:text="首页"
  20.         android:textSize="10sp"
  21.         android:textColor="#ffffff">
  22.     </TextView>

  23. </LinearLayout></font></font>
复制代码
3、fragment布局界面,这里只列出一个,fragment_1.xml:

  1. <font color="#000"><font face="Arial"><span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >

  5.     <ImageView
  6.         android:id="@+id/imageview"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent"
  9.         android:scaleType="fitCenter"
  10.         android:src="@drawable/xianjian01" >
  11.     </ImageView>

  12. </LinearLayout></span></font></font>
复制代码
4、Tab选项的自定义按钮资源文件,列出其中一个按钮,tab_home_btn:

  1. <font color="#000"><font face="Arial"><span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  3.     <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>
  4.     <item android:drawable="@drawable/icon_home_nor"/>

  5. </selector></span></font></font>
复制代码
5、Tab选项按钮背景资源文件,selector_tab_background.xml:

  1. <font color="#000"><font face="Arial"><span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  3.     <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>
  4.     <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/>

  5. </selector></span></font></font>
复制代码
6、主Activity类,MainTabActivity.java:

  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;

  2. import android.os.Bundle;
  3. import android.support.v4.app.FragmentActivity;
  4. import android.support.v4.app.FragmentTabHost;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.TabHost.TabSpec;
  9. import android.widget.TextView;

  10. /**
  11. * @author yangyu
  12. *        功能描述:自定义TabHost
  13. */
  14. public class MainTabActivity extends FragmentActivity{        
  15.         //定义FragmentTabHost对象
  16.         private FragmentTabHost mTabHost;
  17.         
  18.         //定义一个布局
  19.         private LayoutInflater layoutInflater;
  20.                 
  21.         //定义数组来存放Fragment界面
  22.         private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};
  23.         
  24.         //定义数组来存放按钮图片
  25.         private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,R.drawable.tab_selfinfo_btn,
  26.                                                                          R.drawable.tab_square_btn,R.drawable.tab_more_btn};
  27.         
  28.         //Tab选项卡的文字
  29.         private String mTextviewArray[] = {"首页", "消息", "好友", "广场", "更多"};
  30.         
  31.         public void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.main_tab_layout);
  34.         
  35.         initView();
  36.     }
  37.          
  38.         /**
  39.          * 初始化组件
  40.          */
  41.         private void initView(){
  42.                 //实例化布局对象
  43.                 layoutInflater = LayoutInflater.from(this);
  44.                                 
  45.                 //实例化TabHost对象,得到TabHost
  46.                 mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
  47.                 mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);        
  48.                 
  49.                 //得到fragment的个数
  50.                 int count = fragmentArray.length;        
  51.                                 
  52.                 for(int i = 0; i < count; i++){        
  53.                         //为每一个Tab按钮设置图标、文字和内容
  54.                         TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
  55.                         //将Tab按钮添加进Tab选项卡中
  56.                         mTabHost.addTab(tabSpec, fragmentArray[i], null);
  57.                         //设置Tab按钮的背景
  58.                         mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
  59.                 }
  60.         }
  61.                                 
  62.         /**
  63.          * 给Tab按钮设置图标和文字
  64.          */
  65.         private View getTabItemView(int index){
  66.                 View view = layoutInflater.inflate(R.layout.tab_item_view, null);
  67.         
  68.                 ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
  69.                 imageView.setImageResource(mImageViewArray[index]);
  70.                 
  71.                 TextView textView = (TextView) view.findViewById(R.id.textview);                
  72.                 textView.setText(mTextviewArray[index]);
  73.         
  74.                 return view;
  75.         }
  76. }</span>
复制代码
7、Fragment页面,FragmentPage1.java:

  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;

  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;

  7. public class FragmentPage1 extends Fragment{

  8.         @Override
  9.         public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {                
  10.                 return inflater.inflate(R.layout.fragment_1, null);                
  11.         }        
  12. }
  13. </span>
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值