【Android Widget】FragmentTabHost
android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法
效果图(仿新浪微博):

主界面布局文件:
<?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" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" /> <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 MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
private Class<?> fragments[] = { FragmentPage1.class, FragmentPage2.class,
FragmentPage3.class, FragmentPage4.class, FragmentPage5.class };
// 定义数组来存放按钮图片
private int imageIds[] = { 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 };
// Tab选项卡的文字
private String tabLabels[] = { "首页", "消息", "好友", "广场", "更多" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
for (int i = 0; i < 5; i++) {
mTabHost.addTab(
mTabHost.newTabSpec(tabLabels[i]).setIndicator(
getIndicator(i)), fragments[i], null);
}
}
/**
* 获取指示器
*
* @param index
* @return
*/
private View getIndicator(int index) {
TextView textView = (TextView) View.inflate(this,
R.layout.tab_item_view, null);
textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources()
.getDrawable(imageIds[index]), null, null);
textView.setText(tabLabels[index]);
return textView;
}
}
指示器布局indicator_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:background="@drawable/tab_bg_selector"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="12sp" />
每个Fragment很简单 就一个TextView这里就不贴出来了
源码下载:http://files.cnblogs.com/malinkang/FragmentTabHostExample.zip
【Android Widget】FragmentTabHost的更多相关文章
- 【Android Widget】1.TextView
1.创建可被点击的TextView 1.1 在xml中创建可被点击的TextView android:autoLink 是否将符合指定格式的文本转换成可单击的超链接. 属性值可以是如下几个属性值的一个 ...
- 【Android Widget】2.ImageView
1.属性详解 1.1 ScaleType属性详解 ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1 ...
- 【Android - 框架】之GreenDao的使用
上一篇博客([Android - 框架]之ORMLite的使用)中介绍了ORMLite的基本使用,今天我们来研究以下GreenDao的使用. GreenDao和ORMLite一样,都是基于ORM(Ob ...
- 【Android学习】《Android开发视频教程》第一季笔记
视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5 Activity基础概念 1.Android开发技术结构 ...
- 【Android测试】【随笔】模拟双指点击
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5258660.html 手势 看到这个标题,很多人会想一想 ...
- 【Android测试】【随笔】模拟长按电源键
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...
- 【Android - 框架】之Retrofit+RxJava的使用
前几天分别对Retrofit和RxJava进行了总结,这个帖子打算把Retrofit结合RxJava使用的方法总结以下.有还不了解Retrofit或RxJava的朋友可以参考下面的帖子学习~ [And ...
- 【Android Studio】为Android Studio设置HTTP代理
[Android Studio]为Android Studio设置HTTP代理 大陆的墙很厚很高,初次安装Android Studio下载SDK等必定失败,设置代理方法如下: 1. 到androi ...
- 【Android实战】----从Retrofit源代码分析到Java网络编程以及HTTP权威指南想到的
一.简单介绍 接上一篇[Android实战]----基于Retrofit实现多图片/文件.图文上传中曾说非常想搞明确为什么Retrofit那么屌. 近期也看了一些其源代码分析的文章以及亲自查看了源代码 ...
随机推荐
- win8效果的横向布局
有一个月没写过博客了,自己的博客也没有看过,前段时间一直在忙着写代码,公司有一个制漆的产品,与传统纵向布局不一样,要求页面横向布局,类似win8的那种布局效果,最开始,我也没有什么头绪,然后硬着头皮做 ...
- SearchBar简单展示
import UIKit class SearchViewController: UIViewController,UISearchBarDelegate { let SCREEN_WIDTH = U ...
- 使用register_shutdown_function触发写日志,使用fastcgi_finish_request提高响应速度
公司内部的市场管理系统,一直是我一个人维护,最近老是有开发埋怨,内网的账号被人改了密码,账号被解绑了...哈的,错在这还不是一个完整的系统,既没有严格的权限也没有做操作日志呀... 权限现在是准备做在 ...
- Zabbix3.0部署最佳实践
Zabbix3整个web界面做了一个全新的设计. 更多新特性请点击当前字幕查看 笔者QQ:572891887 Linux架构交流群:471443208 1.1Zabbix环境准备 [root@li ...
- 老李分享:robotium3.6与4.0 later 的区别 2
再仔细看了下4.0中的方法: java.util.ArrayList<android.view.View> getCurrentViews() Returns an ...
- MyBatis 源码分析——动态SQL语句
有几年开发经验的程序员应该都有暗骂过原生的SQL语句吧.因为他们不能一句就搞定一个业务,往往还要通过代码来拼接相关的SQL语句.相信大家会理解SQL里面的永真(1=1),永假(1=2)的意义吧.所以m ...
- Java中一个方法只被一个线程调用一次
1.想在运行时抛出异常,终止方法的运行 private final Set<Long> THREADS = new HashSet<>(); public void someM ...
- C++命名空间【转】
本讲基本要求 * 掌握:命名空间的作用及定义:如何使用命名空间. * 了解:使用早期的函数库 重点.难点 ◆命名空间的作用及定义:如何使用命名空间. 在学习本书前面各章时,读者 ...
- Bug 笔记
1.页面返回 400 Bag request: 原因:使用Spring MVC controller的时候,查询数据库:当数据库的数据类型是int型时,Spring MVC在查询的数据匹配给实体类 ...
- 【PAT_Basic日记】1001. 害死人不偿命的(3n+1)猜想
还是觉得代码放这靠谱,会定期的看看和优化代码 #include <stdio.h> #include <stdlib.h> int main() { int n; int co ...