【Android UI設計與開發】第08期:底部菜單欄(三)Fragment+FragmentTabHost實現仿新浪微博底部菜單欄

轉載出處:http://blog.csdn.net/yangyu20121224/article/details/9016223  

轉載,請自覺註明!

    

    

      在上一篇文章中,我們花了大量的篇幅來講解Fragment這個新引進類的使用,目的就是爲了讓大家能夠牢牢的掌握它的使用方法,以便讀者在今後的開發中能夠熟練的使用它。

 


一、實現效果圖




二、項目工程結構

 

三、詳細代碼編寫

 

1、主tab佈局界面,main_tab_layout:

[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="wrap_content"   
  17.         android:background="@drawable/maintab_toolbar_bg">  
  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>  

2、Tab按鈕選項佈局,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:focusable="false"  
  13.         android:padding="3dp"   
  14.         android:src="@drawable/tab_home_btn">  
  15.     </ImageView>  
  16.   
  17.     <TextView  
  18.         android:id="@+id/textview"         
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"   
  21.         android:text="首頁"  
  22.         android:textSize="10sp"  
  23.         android:textColor="#ffffff">  
  24.     </TextView>  
  25.   
  26. </LinearLayout>  

3、fragment佈局界面,這裏只列出一個,fragment_1.xml:

[html] view plaincopy
  1. <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.   
  6.     <ImageView  
  7.         android:id="@+id/imageview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:scaleType="fitCenter"  
  11.         android:src="@drawable/xianjian01" >  
  12.     </ImageView>  
  13.   
  14. </LinearLayout></span>  

4、Tab選項的自定義按鈕資源文件,列出其中一個按鈕,tab_home_btn:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>  
  5.     <item android:drawable="@drawable/icon_home_nor"/>  
  6.   
  7. </selector></span>  

5、Tab選項按鈕背景資源文件,selector_tab_background.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/>  
  6.   
  7. </selector></span>  

6、主Activity類,MainTabActivity.java:

[java] view plaincopy
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;  
  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.  * @author yangyu 
  14.  *  功能描述:自定義TabHost 
  15.  */  
  16. public class MainTabActivity extends FragmentActivity{    
  17.     //定義FragmentTabHost對象  
  18.     private FragmentTabHost mTabHost;  
  19.       
  20.     //定義一個佈局  
  21.     private LayoutInflater layoutInflater;  
  22.           
  23.     //定義數組來存放Fragment界面  
  24.     private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};  
  25.       
  26.     //定義數組來存放按鈕圖片  
  27.     private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,R.drawable.tab_selfinfo_btn,  
  28.                                      R.drawable.tab_square_btn,R.drawable.tab_more_btn};  
  29.       
  30.     //Tab選項卡的文字  
  31.     private String mTextviewArray[] = {"首頁""消息""好友""廣場""更多"};  
  32.       
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main_tab_layout);  
  36.           
  37.         initView();  
  38.     }  
  39.        
  40.     /** 
  41.      * 初始化組件 
  42.      */  
  43.     private void initView(){  
  44.         //實例化佈局對象  
  45.         layoutInflater = LayoutInflater.from(this);  
  46.                   
  47.         //實例化TabHost對象,得到TabHost  
  48.         mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  
  49.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);   
  50.           
  51.         //得到fragment的個數  
  52.         int count = fragmentArray.length;     
  53.                   
  54.         for(int i = 0; i < count; i++){    
  55.             //爲每一個Tab按鈕設置圖標、文字和內容  
  56.             TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));  
  57.             //將Tab按鈕添加進Tab選項卡中  
  58.             mTabHost.addTab(tabSpec, fragmentArray[i], null);  
  59.             //設置Tab按鈕的背景  
  60.             mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);  
  61.         }  
  62.     }  
  63.                   
  64.     /** 
  65.      * 給Tab按鈕設置圖標和文字 
  66.      */  
  67.     private View getTabItemView(int index){  
  68.         View view = layoutInflater.inflate(R.layout.tab_item_view, null);  
  69.       
  70.         ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
  71.         imageView.setImageResource(mImageViewArray[index]);  
  72.           
  73.         TextView textView = (TextView) view.findViewById(R.id.textview);          
  74.         textView.setText(mTextviewArray[index]);  
  75.       
  76.         return view;  
  77.     }  
  78. }</span>  

7、Fragment頁面,FragmentPage1.java:

[java] view plaincopy
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;  
  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 FragmentPage1 extends Fragment{  
  10.   
  11.     @Override  
  12.     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {        
  13.         return inflater.inflate(R.layout.fragment_1, null);       
  14.     }     
  15. }  
  16. </span>  


 


源碼下載地址


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章