DBMNG数据库管理与应用

所有存在都是独创。
当前位置:首页 > 移动应用 > Android

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

PS:这是转载CSDN作者yangyu20121224的文章,真的非常不错.它主要讲述如何通过Fragment碎片实现点击TabHost切换页面.可惜作者很长时间没有更新了,他写的那系列文章都很实用.以后在做Android布局时会用到该篇文章内容,所以转载作为在线笔记.希望对大家也有所帮助吧!
    转载源地址:http://blog.csdn.net/yangyu20121224/article/details/9016223
    
在上一篇文章中,我们花了大量的篇幅来讲解Fragment这个新引进类的使用,目的就是为了让大家能够牢牢的掌握它的使用方法,以便读者在今后的开发中能够熟练的使用它

一、实现效果图

    

二、项目工程结构

 三、详细代码编写

 1、主tab布局界面,main_tab_layout:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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在CODE上查看代码片派生到我的代码片
  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在CODE上查看代码片派生到我的代码片
  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.     <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>  

4、Tab选项的自定义按钮资源文件,列出其中一个按钮,tab_home_btn:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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/icon_home_sel" android:state_selected="true"/>  
  5.     <item android:drawable="@drawable/icon_home_nor"/>  
  6.    
  7. </selector>  

5、Tab选项按钮背景资源文件,selector_tab_background.xml:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  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/home_btn_bg" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/>  
  6.    
  7. </selector>  

6、主Activity类,MainTabActivity.java:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 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,  
  25.                 FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};  
  26.       
  27.     //定义数组来存放按钮图片  
  28.     private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,  
  29.                 R.drawable.tab_selfinfo_btn,R.drawable.tab_square_btn,R.drawable.tab_more_btn};  
  30.       
  31.     //Tab选项卡的文字  
  32.     private String mTextviewArray[] = {"首页","消息","好友","广场","更多"};  
  33.       
  34.     public void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.main_tab_layout);  
  37.           
  38.         initView();  
  39.     }  
  40.        
  41.     /** 
  42.      * 初始化组件 
  43.      */  
  44.     private void initView(){  
  45.         //实例化布局对象  
  46.         layoutInflater = LayoutInflater.from(this);  
  47.                   
  48.         //实例化TabHost对象,得到TabHost  
  49.         mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  
  50.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);   
  51.           
  52.         //得到fragment的个数  
  53.         int count = fragmentArray.length;     
  54.                   
  55.         for(int i = 0; i < count; i++){    
  56.             //为每一个Tab按钮设置图标、文字和内容  
  57.             TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));  
  58.             //将Tab按钮添加进Tab选项卡中  
  59.             mTabHost.addTab(tabSpec, fragmentArray[i], null);  
  60.             //设置Tab按钮的背景  
  61.             mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(  
  62.                                 R.drawable.selector_tab_background);  
  63.         }  
  64.     }  
  65.                   
  66.     /** 
  67.      * 给Tab按钮设置图标和文字 
  68.      */  
  69.     private View getTabItemView(int index){  
  70.         View view = layoutInflater.inflate(R.layout.tab_item_view, null);  
  71.       
  72.         ImageView imageView = (ImageView) view.findViewById(R.id.imageview);  
  73.         imageView.setImageResource(mImageViewArray[index]);  
  74.           
  75.         TextView textView = (TextView) view.findViewById(R.id.textview);          
  76.         textView.setText(mTextviewArray[index]);  
  77.       
  78.         return view;  
  79.     }  
  80. }  

7、Fragment页面,FragmentPage1.java:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 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. }  

    源码下载地址
    PS:该文章主要是在做Android随手拍时转载的,下面也是一些相关优秀的文章。
    [Android UI设计与开发]第09期:底部菜单栏(四)Fragment+PopupWindow仿QQ空间最新版底部菜单栏
    android底部菜单栏demo
    Android开发技巧——实现底部图标文字的导航栏(已更新)
    Android提高十八篇之自定义Menu(TabMenu)

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号