您的位置:首页 > 产品设计 > UI/UE

AndroidUI FragmentTabHost类完成 底部导航布局功能

2014-04-27 21:21 239 查看
FragmentTabHost是在android4.0之后出现的,需要support-v4包提供该类。

如图,类似现在的新版QQ实现的功能,通过不同按钮的点击切换Activity。这里需要FragmentTabHost,Fragment,TabSpec,FragmentActivity类,

FragmentTabHost:是xml中定义的组件,是最主要的类。

Fragment:是选项卡中的activity界面类,通过多个类继承Fragment,并复写onCreate方法,返回View,显示在选项卡的界面上。

TabSpec:通过FragmentTabHost的newTabSpec方法得到,并要为TabSpec指定Fragment的class对象完成 加入到TabHost中。

FragmentActivity:主界面必须继承该类

/**

* 初始化组件,为TabHost定义多个TabSpec,为TabSpec定义Fragment的class对象

*/

private void initView(){

//实例化布局对象

layoutInflater = LayoutInflater.from(this);

//实例化TabHost对象,得到TabHost

mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

//R.id.realtabcontent决定了标签是在上面还是下面显示,FrameLayout在FragmentTabHost上面,标签在上方显示,否则在下方显示。

mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

//得到fragment的个数

int count = fragmentArray.length;

for(int i = 0; i < count; i++){

//为每一个Tab按钮设置图标、文字和内容

TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));

//将Tab按钮添加进Tab选项卡中

mTabHost.addTab(tabSpec, fragmentArray[i], null);

//设置Tab按钮的背景

mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);

}

}

/**

* 给Tab按钮设置图标和文字

*/

private View getTabItemView(int index){

View view = layoutInflater.inflate(R.layout.tab_item_view, null);

ImageView imageView = (ImageView) view.findViewById(R.id.imageview);

imageView.setImageResource(mImageViewArray[index]);

TextView textView = (TextView) view.findViewById(R.id.textview);

textView.setText(mTextviewArray[index]);

return view;

}

上面代码是主要关于FragmentTabHost类的使用和相关类的联系。

源码地址:http://download.csdn.net/detail/qq602298560/7262071
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: