最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到FragmentActivity上。tab可以放在最上面也可以放在最下面
由以下布局文件main.xml<FrameLayout>的位置决定
<?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常用控件及对应Robotium API
最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...
- 常用的基本控件 android常用控件
1.TextView:(文本框):不能编辑 android:textColor="@color/tv_show_color" 字体颜色 android:textSize ...
- Android常用控件
Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...
- Android常用控件之GridView使用BaseAdapter
我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...
- Android常用控件之RatingBar的使用
RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...
- android常用控件的使用方法
引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...
- Android 常用控件的介绍
http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...
- Android常用控件之GridView与ExpandableListView的用法
概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每 ...
- Android常用控件之Fragment仿Android4.0设置界面
Fragment是Android3.0新增的概念,是碎片的意思,它和Activity很相像,用来在一个Activity中描述一些行为或部分用户界面:使用多个Fragment可以在一个单独的Activi ...
随机推荐
- HDU 3397 Sequence operation (区间合并,操作比较多)
费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...
- [C++]默认构造函数
默认构造函数(default constructor)就是在没有显示提供初始化式时调用的构造函数.它由不带参数的构造函数,或者为所有的形参提供默认实参的构造函数定义.若个定义某个类的变量时没有提供初始 ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- Scanner演示
import java.util.Scanner; /** *Scanner演示 */ public class ScannerDemo{ public st ...
- eclipse 中忽略jsp, xml文件中的报错信息
有的时候, 在eclipse中, jsp, xml 文件时运行的好好的, 可是就是在eclipse中报错, 虽然不影响功能, 但看起来很烦, 去掉这些错误警告的方法是: Windows-Prefere ...
- 261. Graph Valid Tree
题目: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nod ...
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
操作系统:CentOS6.5 本文档主要讲解,如何在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群服务器,Nginx负责负载均衡,Tomcat负责实际服务,Memc ...
- 8、SpringMVC源码分析(3):分析ModelAndView的形成过程
首先,我们还是从DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throw ...
- PHP程序员的40点陋习
1.不写注释 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行sql前不执行编码和安全检测 8.不使用测 ...
- <<c 和指针 >> 部分笔记。
最近竟然对指针有些迷惑了,分不清指针的指向.废话少说,复习.(下面内容来自<<c和指针>>) =指针 ==内存和地址 尽管一个字包含了4个字节,它仍然只有一个地址.至于是最左边 ...