最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到FragmentActivity上。tab可以放在最上面也可以放在最下面

 
 
由以下布局文件main.xml<FrameLayout>的位置决定
 
[html]  
<?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">  
      
    <!-- 这个布局决定了标签在上面还是在下面显示 -->  
    <FrameLayout   
        android:id="@+id/realtabcontent"  
        android:layout_width="match_parent"  
        android:layout_height="0dip"  
        android:layout_weight="1" />  
      
    <android.support.v4.app.FragmentTabHost  
        android:id="@android:id/tabhost"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content">  
          
        <TabWidget   
            android:id="@android:id/tabs"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:orientation="horizontal"/>  
    </android.support.v4.app.FragmentTabHost>  
      
</LinearLayout>  
 
创建一个类继承FragmentActivity
 
[java]  
package com.example.tabhostdemo;  
  
import android.os.Bundle;  
import android.support.v4.app.FragmentActivity;  
import android.support.v4.app.FragmentTabHost;  
import android.view.View;  
import android.widget.TextView;  
  
import com.example.tabhost.FirstFragment;  
import com.example.tabhost.ThirdFragment;  
import com.example.tabhost.secondFragment;  
  
public class MainActivity extends FragmentActivity {  
  
    private FragmentTabHost mTabHost = null;;  
    private View indicator = null;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.main);  
  
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  
        // 添加tab名称和图标  
        indicator = getIndicatorView("我的联系人", R.layout.mycontact_indicator);  
        mTabHost.addTab(mTabHost.newTabSpec("myContact")  
                .setIndicator(indicator), FirstFragment.class, null);  
  
        indicator = getIndicatorView("陌生人", R.layout.strangercontact_indicator);  
        mTabHost.addTab(  
                mTabHost.newTabSpec("stranger").setIndicator(indicator),  
                secondFragment.class, null);  
  
        indicator = getIndicatorView("常联系人", R.layout.alwayscontact_indicator);  
        mTabHost.addTab(  
                mTabHost.newTabSpec("alwaysContact").setIndicator(indicator),  
                ThirdFragment.class, null);  
    }  
  
    private View getIndicatorView(String name, int layoutId) {  
        View v = getLayoutInflater().inflate(layoutId, null);  
        TextView tv = (TextView) v.findViewById(R.id.tabText);  
        tv.setText(name);  
        return v;  
    }  
  
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        mTabHost = null;  
    }  
}  
第一个Tab的布局文件    存放两张图片,字体颜色
alwayscontact_indicator.xml文件
[html] view plaincopy
<?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:layout_gravity="center"   
    android:gravity="center">  
  
    <TextView   
        android:id="@+id/tabText"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:focusable="true"  
        android:drawableTop="@drawable/mycontact_selector"  
        android:textColor="@drawable/tabitem_txt_sel"/>  
</LinearLayout>  
 
mycontact_selector.xml文件
[html]  
<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >      
    <!-- Non focused states -->  
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/mycontact" />  
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
    <!-- Focused states -->  
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/mycontact_sel" />  
    <!-- Pressed -->  
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/mycontact_sel" />  
    <item android:state_pressed="true" android:drawable="@drawable/mycontact_sel" />    
</selector>  
 
tabitem_txt_sel.xml文件
[html] 
<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android" >      
    <!-- Non focused states -->  
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:color="#A4A4A4" />  
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:color="#00A3F5" />  
    <!-- Focused states -->  
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:color="#00A3F5" />  
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:color="#00A3F5" />  
    <!-- Pressed -->  
    <item android:state_selected="true" android:state_pressed="true" android:color="#00A3F5" />  
    <item android:state_pressed="true" android:color="#00A3F5" />   
</selector>  
其它的tab文件定义也是类似的,看下最后的效果图
 

Android常用控件之FragmentTabHost的使用的更多相关文章

  1. Android常用控件及对应Robotium API

    最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...

  2. 常用的基本控件 android常用控件

    1.TextView:(文本框):不能编辑    android:textColor="@color/tv_show_color" 字体颜色    android:textSize ...

  3. Android常用控件

     Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...

  4. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  5. Android常用控件之RatingBar的使用

    RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...

  6. android常用控件的使用方法

    引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...

  7. Android 常用控件的介绍

    http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...

  8. Android常用控件之GridView与ExpandableListView的用法

    概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每 ...

  9. Android常用控件之Fragment仿Android4.0设置界面

    Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像,用来在一个Activity中描述一些行为或部分用户界面:使用多个Fragment可以在一个单独的Activi ...

随机推荐

  1. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  2. 心情记录&考试总结 3.30

    并不知道现在要干什么,本人像是一只大颓狗 Em..怎么说呢,今天考完了一场奇怪的试 准确的说,画风很不正常的试 第一题集体爆零 第二题暴力20分 第三题暴力40分,乱搞有加成 改题的话, 第一题有奇怪 ...

  3. lintcode: 二叉查找树中搜索区间

    题目 二叉查找树中搜索区间 给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点.找到树中所有值在 k1 到 k2 范围内的节点.即打印所有x (k1 <= x <= ...

  4. SpringMVC学习总结(五)——SpringMVC文件上传例子

    这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml <?xml version="1.0" e ...

  5. Java Logger(java日志)

    目录 1. 简介2. 安装3. log4j基本概念3.1. Logger3.2. Appender3.2.1. 使用ConsoleAppender3.2.2. 使用FileAppender3.2.3. ...

  6. 数据类型演示DataTypeDemo

    /***数据类型演示*/public class DataTypeDemo{ public static void main(String[] args){  //直接赋予的值,称为字面量  //by ...

  7. HotSwap和JRebel原理

    HotSwap和JRebel原理 HotSwap和Instrumentation 在2002年的时候,Sun在Java 1.4的JVM中引入了一种新的被称作HotSwap的实验性技术,这一技术被合成到 ...

  8. chmod u+x 脚本文件

    [root@ossec-server Shell]# chmod u+x whologged.sh解释: chmod:改变权限 u:文件所有用户 +x: 增加可执行权限 [root@ossec-ser ...

  9. java对称加密(AES)

    java对称加密(AES) 博客分类: Java javaAES对称加密  /** * AESHelper.java * cn.com.songjy.test * * Function: TODO * ...

  10. 盘点PHP编程常见失误

    概述:本文盘点PHP开发者在编码时,容易忽略或不注意引起的小失误与错误. 变量声明 如果在一条语句中声明一个变量,如下所示:$var='value';编译器首先会求出语句右半部分的值,恰恰正是语句的这 ...