您的位置:首页 > 其它

FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity

2014-06-28 02:22 232 查看
自Android3.2之后,TabActibvity被弃用(Deprecated),取而代之的是FragmentActivity,因为Fragment比Activiy更灵活,消耗的资源更小,完全能够满足TabActivity的效果,所以直接替代之。原来的TabActibvity+TabHost+Activity那套还可以用,不过强烈建议改用FragmentActivity+FragmentTabHost+Fragement

FragmentTabHost用法:

1. 定义FragmentActivity的layout:

[html] view
plaincopy

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<FrameLayout

android:id="@+id/realtabcontent"

android:layout_width="fill_parent"

android:layout_height="0dip"

android:layout_weight="1" />

<android.support.v4.app.FragmentTabHost

android:id="@android:id/tabhost"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/maintab_toolbar_bg">

<FrameLayout

android:id="@android:id/tabcontent"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_weight="0" />

</android.support.v4.app.FragmentTabHost>

</LinearLayout>

2. 必须继承FragmentActivity

[java] view
plaincopy

public class MainTabActivity extends FragmentActivity{

//定义FragmentTabHost对象

private FragmentTabHost mTabHost;

3. 得到FragmentTabHost对象

[java] view
plaincopy

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

4. 初始化FragmentTabHost对象

[java] view
plaincopy

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

注意,这里的R.id.realtabcontent可以是任一个ViewGroup或其子类的对象id,比如LinearLayout。其实际作用就是个容器,Tab切换时,当前Tab对应的Fragment会被加入到这个ViewGroup作为其子View

5.按顺序添加每个Tab页

[java] view
plaincopy

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

TabSpec tabSpec = mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemView);

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

mTabHost.addTab(tabSpec, fragmentPage1.class, null);

//设置Tab按钮的背景

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

注意,mTabHost.newTabSpec("TAG1").setIndicator(yourTabItemview);这里的"TAG1"其实没什么什么意思,区分一下每个tab就好。
重点在于setIndicator函数,其有三个不同的实现,也就是说,你可以使用三种方式来定义你的Tab的风格:

[java] view
plaincopy

//只使用要文字标识tab

TabHost.TabSpec.setIndicator(CharSequence label)

//使用文字+icon标识tab

TabHost.TabSpec.setIndicator(CharSequence label, Drawable icon)

//使用自定义的View表示tab

TabHost.TabSpec.setIndicator(View view)

前面两种tab风格,是我们在绝大多数tabhost的范例中看到的风格(Holo风格),也就是当前选择的tab下面会有类似于滚动条的一个高亮显示的一个线条(indicator),很多时候我们不需要它,比如微信风格的Tab。这时候你就可以使用第三种方式来自定义你的Tab风格,你可以实现任何样式的Tab:

[java] view
plaincopy

View yourTabItemView = 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]);

另外,fragmentPage1.class是一个继承自Fragment的类,在切换Tab时,会被动态实例化,并且add到R.id.realtabcontent这个内容容器中显示

完成上面几点,一个简单的FragementActivity+FragmentTabHost+Fragment效果就出来了,接下来讲如何调整Tab停靠在顶部还是底部。

当R.id.realtabcontent与R.id.tabhost不在一个布局文件时,默认Tab在上TabContent在下,不能调整TabContent与Tab。

当R.id.realtabcontent与R.id.tabhost在一个布局文件时,如果R.id.realtabcontent在R.id.tabhost上面,那么Tab将会在TabContent下面,也就是说R.id.realtabcontent与R.id.tabhost的相对位置决定了选页在上还是在下。

不要在布局文件中给FragmentTabHost 设置子View,否则子View将显示在以Tab左上角为坐标0点的View中。给R.id.realtabcontent设置 android:layout_weight="1",因为默认时Tabcontent高度是wrap_content并且不能被调整,当Tab在Tabcontent下面并且显示的View不足把Tab挤到底部时,Tab会挂在显示的View的末尾,设置后Tabcontent就会被填充满了。

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