FragmentTabHost实现底部切换简单应用

今天项目中遇到了一个FragmentTabHost实现底部切换,FragmentTabHost来自于android.support.v4.app这个包下,继承自  TabHost ,并且实现了  TabHost.OnTabChangeListener 接口。文档上也直接放了两个例子。一个是在Activity下包含多个fragment,注意这个activity是v4包下的  



首先看下xml布局
<? xml version= "1.0"  encoding= "utf-8" ?>
<LinearLayout  xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: tools = "http://schemas.android.com/tools"
    android :layout_width= "match_parent"
    android :layout_height= "match_parent"
    android :orientation= "vertical"
    tools :context= "com.aima.jiangtao.libraryproject.activity.FragmentTabHostActivity" >
    <FrameLayout
        android :id= "@+id/realtabcontent"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content"
        android :layout_weight= "1"  />

    <android.support.v4.app.FragmentTabHost
        android :id= "@+id/tabhost"
        android :layout_width= "match_parent"
        android :layout_height= "wrap_content"
        android :background= "@drawable/bg_tabhost_bg" >

        <FrameLayout
            android :id= "@+id/tabcontent"
            android :layout_width= "0dp"
            android :layout_height= "0dp"
            android :layout_weight= "0"  />
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

主类Activity 这里我们需要继承FragmentActivity
package com.aima.jiangtao.libraryproject.activity ;

import android.os.Bundle ;
import android.support.v4.app.FragmentActivity ;
import android.support.v4.app.FragmentTabHost ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.widget.ImageView ;
import android.widget.TabHost ;
import android.widget.TextView ;

import com.aima.jiangtao.libraryproject.R ;
import com.aima.jiangtao.libraryproject.fragment. CommunityFragment ;
import com.aima.jiangtao.libraryproject.fragment.HomeFragment ;
import com.aima.jiangtao.libraryproject.fragment.PersonFragment ;

/**
 *TabHost底部切换Activity
 */
public class FragmentTabHostActivity  extends FragmentActivity {
    /**
     * tabFragmentHost
     */
    private FragmentTabHost  tabHost ;
    /**
     * 布局加载器
     */
    private LayoutInflater  mLayoutInflater ;
    /**
     * Fragment集合 数组界面
     */
    private Class[]  mFragments= {HomeFragment. class,  CommunityFragment. class, PersonFragment. class} ;

    /**
     *存放底部图片
     */
    private int []  mImgArray = {R.drawable. tab_home_btn ,R.drawable. tab_message_btn ,R.drawable. tab_more_btn} ;

    private String []  mTextArray = { "首页" , "圈子" , "个人"} ;
    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_fragment_tab_host) ;
        initView() ;
    }

    /**
     * 初始化控件
     */
    private void  initView(){
        mLayoutInflater = LayoutInflater. from( this) ;
        tabHost = (FragmentTabHost) findViewById(R.id. tabhost) ;
        tabHost.setup( this,getSupportFragmentManager() , R.id. realtabcontent) ;
        //得到Fragment个数
        int count =  mFragments. length ;
        for ( int i =  0 i < count  i++){
            //通过TabSpec设置按钮图标、文字、内容
            TabHost.TabSpec tabSpec =  tabHost.newTabSpec( mTextArray[i]).setIndicator(getTabItemView(i)) ;
            //将Tab按钮添加进Tab选项卡中
            tabHost.addTab(tabSpec , mFragments[i] ,null) ;


        }
    }

    /***
     * 把每个底部按钮按照Item设置图片和文字
     *  @param  index
      @return
      */
    private View  getTabItemView( int index){
        View view =  mLayoutInflater.inflate(R.layout. tab_item_view ,null) ;
        ImageView imageView = (ImageView) view.findViewById(R.id. tab_item_img) ;
        imageView.setImageResource( mImgArray[index]) ;
        TextView textView = (TextView) view.findViewById(R.id. tab_item_txt) ;
        textView.setText( mTextArray[index]) ;
        return view ;
    }

}


button选项的xml文件(其他的跟这个一样自己根据需要设置图片,图片就上传了,自己找一个就行了)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

</selector>



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值