Android 底部TabActivity(1)——FragmentActivity

来源:互联网 发布:消除图片马赛克软件 编辑:程序博客网 时间:2024/06/06 02:24

先看看效果图:

第一篇Tab系列的文章首先实现这种风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生相应的变化,当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深的灰色,更加凸显当前页的效果,所以我比较这种类型。在这里文字的变化我没处理,如果变色使用个selector就解决了,这里不再赘述。


再看一下整个Project的结构,如下



下面逐一介绍一下实现过程,具体实现还是看注释吧,代码也不是很多,就不啰嗦了。

step1:首先是主界面MainTabActivity.java

[java] view plaincopy
  1. package sun.geoffery.fragmenttabhost;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.app.FragmentTabHost;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.ImageView;  
  9. import android.widget.TabHost.TabSpec;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * All rights Reserved, Designed By GeofferySun  
  14.  * @Title:  MainTabActivity.java 
  15.  * @Package sun.geoffery.fragmenttabhost 
  16.  * @Description:自定义TabHost 
  17.  * @author: GeofferySun    
  18.  * @date:   2014-9-28 下午11:33:15 
  19.  * @version V1.0 
  20.  */  
  21. public class MainTabActivity extends FragmentActivity {  
  22.     // 定义FragmentTabHost对象  
  23.     private FragmentTabHost mTabHost;  
  24.   
  25.     // 定义一个布局  
  26.     private LayoutInflater mInflater;  
  27.   
  28.     // 定义数组来存放Fragment界面  
  29.     private Class mFragmentAry[] = { FragmentPage0.class, FragmentPage1.class,  
  30.             FragmentPage2.class, FragmentPage3.class, FragmentPage4.class };  
  31.   
  32.     // 定义数组来存放按钮图片  
  33.     private int mImgAry[] = { R.drawable.sl_rbtn_home,  
  34.             R.drawable.sl_rbtn_atme,  
  35.             R.drawable.sl_rbtn_msg,  
  36.             R.drawable.sl_rbtn_square,  
  37.             R.drawable.sl_rbtn_data };  
  38.   
  39.     // Tab选项卡的文字  
  40.     private String mTxtAry[] = { "首页""@我""消息""广场""资料" };  
  41.   
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.main_tab_layout);  
  45.   
  46.         initView();  
  47.     }  
  48.   
  49.     /** 
  50.      * 初始化组件 
  51.      */  
  52.     private void initView() {  
  53.         // 实例化布局对象  
  54.         mInflater = LayoutInflater.from(this);  
  55.   
  56.         // 实例化TabHost对象,得到TabHost  
  57.         mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
  58.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  59.   
  60.         // 得到fragment的个数  
  61.         int count = mFragmentAry.length;  
  62.   
  63.         for (int i = 0; i < count; i++) {  
  64.             // 为每一个Tab按钮设置图标、文字和内容  
  65.             TabSpec tabSpec = mTabHost.newTabSpec(mTxtAry[i]).setIndicator(getTabItemView(i));  
  66.             // 将Tab按钮添加进Tab选项卡中  
  67.             mTabHost.addTab(tabSpec, mFragmentAry[i], null);  
  68.             // 设置Tab按钮的背景  
  69.             mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * 给Tab按钮设置图标和文字 
  75.      * @param index 
  76.      * @return 
  77.      */  
  78.     private View getTabItemView(int index) {  
  79.         View view = mInflater.inflate(R.layout.tab_item_view, null);  
  80.   
  81.         ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
  82.         imageView.setImageResource(mImgAry[index]);  
  83.   
  84.         TextView textView = (TextView) view.findViewById(R.id.textview);  
  85.         textView.setText(mTxtAry[index]);  
  86.   
  87.         return view;  
  88.     }  
  89. }  

step2:每一个标签页对应的页面FragmentPage0.java

[java] view plaincopy
  1. package sun.geoffery.fragmenttabhost;  
  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 FragmentPage0 extends Fragment {  
  10.   
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  13.             Bundle savedInstanceState) {  
  14.         return inflater.inflate(R.layout.fragment_0, null);  
  15.     }  
  16. }  
step3:单选标签的背板文件selector_tab_background.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/bg_bottombar_active" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/bg_bottombar_active" android:state_selected="true"/>  
  6.   
  7. </selector>  
step4:标签的图标sl_rbtn_atme.xml,有点击效果就要这么搞

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?><!-- tab栏按钮 -->  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/sl_rbtn_atme_on" android:state_selected="true" />  
  5.     <item android:drawable="@drawable/sl_rbtn_atme_off" />  
  6.   
  7. </selector>  

step5:主界面布局main_tab_layout.xml

[html] view plaincopy
  1. <?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.   
  7.     <FrameLayout  
  8.         android:id="@+id/realtabcontent"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="0dip"  
  11.         android:layout_weight="1" />  
  12.   
  13.     <android.support.v4.app.FragmentTabHost  
  14.         android:id="@android:id/tabhost"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="60dp"  
  17.         android:background="@drawable/bg_bottombar" >  
  18.   
  19.         <FrameLayout  
  20.             android:id="@android:id/tabcontent"  
  21.             android:layout_width="0dp"  
  22.             android:layout_height="0dp"  
  23.             android:layout_weight="0" />  
  24.     </android.support.v4.app.FragmentTabHost>  
  25.   
  26. </LinearLayout>  
step6:每个标签的布局tab_item_view.xml,上边一个图标,下边一个文字

[html] view plaincopy
  1. <?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.   
  8.     <ImageView  
  9.         android:id="@+id/imageview"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:contentDescription="@string/app_name"  
  13.         android:focusable="false"  
  14.         android:padding="3dp"  
  15.         android:src="@drawable/ic_launcher" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/textview"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:paddingBottom="7dp"  
  22.         android:paddingTop="3dp"  
  23.         android:text="@string/app_name"  
  24.         android:textColor="#ffffff"  
  25.         android:textSize="12sp" />  
  26.   
  27. </LinearLayout>  
step7:每个标签对应页面的布局fragment_0.xml

[html] view plaincopy
  1. <?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.         
  6.     <TextView   
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:text="首页"  
  10.         android:gravity="center"  
  11.         android:textSize="20sp"  
  12.         android:textColor="#403901"/>   
  13.         
  14. </LinearLayout>  
0 0
原创粉丝点击