FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
来源:互联网 发布:windows vista重装 编辑:程序博客网 时间:2024/05/19 21:17
近段时间发现自己很少写些原创文章了,倒是转载了很多文章,转载的原因其实都是工作当中遇到的技术积累总结,
转载权当是做一个记事本功能,避免自己遗忘。
=================================================================================
原博客链接: http://blog.csdn.net/olevin/article/details/35432421?utm_source=tuicool
自Android3.2之后,TabActibvity被弃用(Deprecated),取而代之的是FragmentActivity,因为Fragment比Activiy更灵活,消耗的资源更小,完全能够满足TabActivity的效果,所以直接替代之。原来的TabActibvity+TabHost+Activity那套还可以用,不过强烈建议改用FragmentActivity+FragmentTabHost+Fragement
FragmentTabHost用法:
1. 定义FragmentActivity的layout:
- <?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>
- public class MainTabActivity extends FragmentActivity{
- //定义FragmentTabHost对象
- private FragmentTabHost mTabHost;
3. 得到FragmentTabHost对象
- mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
4. 初始化FragmentTabHost对象
- mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
注意,这里的R.id.realtabcontent可以是任一个ViewGroup或其子类的对象id,比如LinearLayout。其实际作用就是个容器,Tab切换时,当前Tab对应的Fragment会被加入到这个ViewGroup作为其子View
5.按顺序添加每个Tab页
- //为每一个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的风格:- //只使用要文字标识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:
- 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
- FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
- FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
- 用FragmentActivity +FragmentTabHost 替换原有的tabhost
- Android Tabhost with FragmentActivity
- Android TabHost与FragmentActivity
- Activity、FragmentActivity
- 使用FragmentTabhost代替Tabhost
- TabHost 和 FragmentTabHost面面观
- TabHost 和 FragmentTabHost
- TabHost 和 FragmentTabHost
- TabHost 和 FragmentTabHost
- 使用FragmentTabhost代替Tabhost
- TabHost 和 FragmentTabHost
- Activity和Fragement生命周期
- FragmentTabhost+fragment+viewpager+FragmentActivity的使用
- (转)使用FragmentTabhost代替Tabhost
- Fragement替换Tabhost,实现侧滑功能
- FragmentActivity、Activity、Fragment关系
- 树 前序遍历,中序遍历,后序遍历
- 设计模式:14 观察者模式
- GdiPlus使用方法
- ios8新特性widget开发(自定义的通知栏样式)
- HTTP POST和GET区别简介
- FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity
- Uncaught TypeError: Cannot read property 'options' of undefined 错误
- UI_Menu_Options Menu
- 动态调用WebService的方法
- AT指令操作GPRS Modem实现网络传输
- 实现文字部分加粗
- Android之查看外部依赖jar的源码
- Copy List with Random Pointer
- Embedded Jetty+Spring MVC + Maven (IntelliJ 14) [三种简单启动方法 未完待续复杂配置]: