今天项目中遇到了一个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>
<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 ;
}
}
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>