如今Fragment使用越来越广了,尽管Fragment寄生在Activity下。可是它的出现对于开发人员来说是一件很幸运的事,使开发的效率更高效了。好了以下就说说 FragmentTabhost的使用。由于Tabhost已经不推荐使用了,如今一般都使用FragmentTabhost!

我本身也个菜鸟,就是帮帮新手,由于Fragment是3.0才出现,为了避免3.0以下的使用不了,所以我们要用v4包来支持。不要倒错包哦。大神勿喷!

一:首先我们看看XML:

1.activity_main.xml

<?

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/bg_tabhost_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.tab_item_view.xml

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

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="3dp"
android:src="@drawable/tab_home_btn">
</ImageView> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="10sp"
android:textColor="#ffffff">
</TextView> </LinearLayout>

3.fragment1.xml 就贴一个Fragment XML吧!其它的几个都一样。仅仅是颜色不一样,呵呵。

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

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FBB55D" > </LinearLayout>

ok,XML先写完了,那我们看看代码吧!

4.MainActivity



package com.example.fragmenttabhost;

import android.os.Bundle;
import android.support.v4.app.Fragment;
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.TabSpec;
import android.widget.TextView; import com.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;
import com.example.fragment.Fragment4;
import com.example.fragment.Fragment5; /**
*
* @author zqy
*
*/
public class MainActivity extends FragmentActivity {
/**
* FragmentTabhost
*/
private FragmentTabHost mTabHost; /**
* 布局填充器
*
*/
private LayoutInflater mLayoutInflater; /**
* Fragment数组界面
*
*/
private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
Fragment3.class, Fragment4.class, Fragment5.class };
/**
* 存放图片数组
*
*/
private int mImageArray[] = { R.drawable.tab_home_btn,
R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
R.drawable.tab_square_btn, R.drawable.tab_more_btn }; /**
* 选修卡文字
*
*/
private String mTextArray[] = { "首页", "消息", "好友", "搜索", "很多其它" };
/**
*
*
*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView();
} /**
* 初始化组件
*/
private void initView() {
mLayoutInflater = LayoutInflater.from(this); // 找到TabHost
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 得到fragment的个数
int count = mFragmentArray.length;
for (int i = 0; i < count; i++) {
// 给每一个Tabbutton设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
.setIndicator(getTabItemView(i));
// 将Tabbutton加入进Tab选项卡中
mTabHost.addTab(tabSpec, mFragmentArray[i], null);
// 设置Tabbutton的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.selector_tab_background);
}
} /**
*
* 给每一个Tabbutton设置图标和文字
*/
private View getTabItemView(int index) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextArray[index]); return view;
} }

5.Fragment1.java  Fragment其它几个都一样,指只是XML不一样!

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import com.example.fragmenttabhost.R; public class Fragment1 extends Fragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null);
}
}

OK 基本上写完了,让我们看看效果!

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGlhb3l1YW41MTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">



哈哈,效果还算能够!好了,去吃饭了!

资源下载地址

使用FragmentTabhost取代Tabhost的更多相关文章

  1. ViewPagerIndicator 取代TabHost,实现滑动tab,引导页等效果

    https://github.com/eltld/ViewPagerIndicator 取代TabHost,实现滑动tab,引导页等效果

  2. FragmentTabHost

    FragmentTabHost public class FragmentTabHost  extends TabHost implements TabHost.OnTabChangeListener ...

  3. Android的TabHost组件-android的学习之旅(四十)

    TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...

  4. FragmentTabHost用法

    FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |--- ...

  5. FragmentTabHost切换Fragment时保存状态,避免切换Fragment走onCreateView和onDestroyView方法;

    FragmentTabHost这个控件每次切换Fragment,都会走Fragment的onCreateView和onDestroyView方法,多以每次切换都会创建和销毁Fragment实例,先来看 ...

  6. Fragmenttabhost的使用教程

    1.准备tab的图标,放到mipmap目录下面,大小64x64,准备2种,一种是选中的,一种是未选中的,如下图 2.重写fragmentabhost,防止调用fragment每次点击tab都要重新调用 ...

  7. Android重写FragmentTabHost来实现状态保存

    近期要做一个类似QQ底部有气泡的功能,试了几个方案不太好.我想非常多开发人员使用TabHost都会知道它不保存状态.每次都要又一次载入布局.为了保存状态,使用RadioGroup来实现.状态是能够保存 ...

  8. 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?

    目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...

  9. 59.Android开源项目及库 (转)

    转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...

随机推荐

  1. Android智能手机屏蔽电话与屏蔽安装软件功能

    近期做一些项目.须要对手机进行屏蔽自己的固有的功能.在此记录. Android屏蔽电话功能主要是卸载掉Phone.apk. 屏蔽安装软件功能主要是卸载掉PackageInstall.apk 以下以三星 ...

  2. Android 应用开发推荐书单

    本文由 伯乐在线 - zerob13 翻译自 fromdev.欢迎加入Android小组.转载请参见文章末尾处的要求. Android 已经成为了世界上最受欢迎的操作系统之一.成千上万的智能手机和平板 ...

  3. 使用css3写一朵云

  4. Spring MVC 学习笔记 json格式的输入和输出

    Spring mvc处理json需要使用jackson的类库,因此为支持json格式的输入输出需要先修改pom.xml增加jackson包的引用 <!-- json --> <dep ...

  5. boost.xml_parser中文字符问题

    当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...

  6. Oracle闪回flashback总结

    1.说明: Ø  采用的技术. 使用的是多个技术. 1.      闪回日志 2.      回收站 3.      回滚段 无法使用回收站的操作 Drop table xxx purge; Drop ...

  7. 一起C语言中程序时序问题的排查过程

    [文章摘要] 对于由多个模块协同工作的软件来说,程序处理的时序是很重要的.当消息处理的顺序出现混乱时,程序就会出现异常. 本文基于作者的实际项目经验.对软件模块之间的时序问题进行了具体的分析,为相关软 ...

  8. Android ListView条目全选功能,不用checkbox实现!

    大家好,翻了翻曾经的笔记,发现了一个我特别标记的功能,那就是ListView全选功能,顿时想起了我那个时候苦逼的生涯,因为我大学机械出身,大学毕业了都不知道什么叫代码,在58干了一段销售.实在是干不下 ...

  9. Eclipse选项卡式的属性视图(The Eclipse Tabbed Properties View)

    Eclipse工作台提供了一个性能视图用于查看(和/或编辑)选定项目的属性. 在本文中,您将学习怎样使用选项卡式的属性视图创建一个性能增强的用户界面视图. 1.引言 Eclipse工作台提供了一个属性 ...

  10. java常用内存设置

    Java虚拟机具有一个堆,是运行时的数据区域,所有类实例和数组内存均从此处分配.堆是在java虚拟机启动时创建的. 堆是留给开发人员用的内存区域.非堆就是JVM留给自己用的(方法区,JVM内部处理或优 ...