FragmentTabHost的基本用法
开通博客以来已经约莫1个月了。几次想提笔写写东西,但总是由于各种各样的原因并没有开始。现在,年假刚结束,项目也还没有开始,但最终促使我写这篇博客的是,看了一篇博友写的新年计划,说是要在新的一年中写50篇博客,我也心血来潮的定下了这样的目标。把年前项目中用到的FragmentTabHost在这里总结一下。
现在市面上app的主流框架大体分为两种:一种是在主界面点击菜单按钮,之后会滑出侧滑菜单,之后进入到各个模块,还有一种是在主界面的下面放置若干个tab按钮,点击按钮,切换到不同的模块。今天要讲的就是第二种的实现方式之一的FragmentTabHost.
FragmentTabHost来自于android.support.v4.app这个包下,继承自TabHost,作为android4.0的控件。好了,废话还是少说为妙。
下面是主界面的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.itrui.searchs.MainActivity" > <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#D0D0D0">
</TextView>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFFFFF"
android:layout_gravity="bottom"
android:padding="10dp"
></TabWidget>
</LinearLayout>
</android.support.v4.app.FragmentTabHost> </RelativeLayout>
控件的命名是固定的不能随便更改。控件的id必须是Android提供的标准id, 即"@android:id"
Tab的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/layout_ancor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="vertical"
android:padding="2dp"
android:paddingBottom="15dp"
android:paddingTop="10dp">
<ImageView
android:id="@+id/img_tab_pic"
android:layout_width="32dp"
android:layout_height="32dp"
/>
</LinearLayout> </RelativeLayout>
此布局文件是一张图片,当然你也可以根据自己的需求来添加其他的控件,比如文字之类的控件
其中之一的Fragment的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/fragment_main_frist_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:layout_marginLeft="80dp"
android:text="poi搜索"/>
<ImageView
android:id="@+id/iv_baidu_dingwei"
android:layout_width="32dp"
android:layout_height="32dp"
android:paddingTop="10dp"
android:paddingRight="5dp"
android:layout_alignParentRight="true"
android:src="@drawable/baidumap"/>
</RelativeLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是主界面的第一个fragment"/> </LinearLayout>
当然你也可以同一个fragment实现复用。
主页面的代码:
public class MainActivity extends FragmentActivity { private FragmentTabHost myTabhost;
//Tab图片
private int mImages[] = {
R.drawable.tab_assistant_gray,R.drawable.tab_center_gray,R.drawable.tab_contest_gray,R.drawable.tab_counter_gray
};
//标记
private String mFragmentTags[] ={
"第一个","第二个","第三个","第四个"
};
//加载的Fragment
private Class mFragment[] ={ MainFristFragment.class,MainSecondFragment.class,MainThridFragment.class,MainFristFragment.class };
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabHost();
}
private void initTabHost() {
myTabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);
myTabhost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
//去掉分割线
myTabhost.getTabWidget().setDividerDrawable(null);
for(int i = 0;i<mImages.length;i++){
//对Tab按钮添加标记和图片
TabSpec tabSpec = myTabhost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i));
//添加Fragment
myTabhost.addTab(tabSpec,mFragment[i],null);
myTabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.white); } } //获取图片资源
private View getImageView(int index){
View view = getLayoutInflater().inflate(R.layout.tab_title, null);
ImageView imageView = (ImageView) view.findViewById(R.id.img_tab_pic);
imageView.setImageResource(mImages[index]);
return view; } }
在onCreat()中执行 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);语句的作用是让手机屏幕保持一种不暗不关闭的效果。通常的应用场景是视频播放器。
总结:
在FragmentTabHost这一个布局中包裹着FrameLayout(也可以换成其他布局主要作用是存放fragment)和TabWidget控件。
执行语句:
myTabhost.setup(this, getSupportFragmentManager(),android.R.id.tabhost);
去掉分割线
myTabhost.getTabWidget().setDividerDrawable(null);
向FragmentTabHost中添加标识和添加图标
TabSpec tabSpec = myTabhost.newTabSpec(mFragmentTags[i]).setIndicator(getImageView(i));
将对应的fragment添加到控件中去
myTabhost.addTab(tabSpec,mFragment[i],null);
其实还是蛮好用的一个控件。
FragmentTabHost的基本用法的更多相关文章
- FragmentTabHost用法
FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |--- ...
- 【Android Widget】FragmentTabHost
android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...
- FragmentTabHost的应用
原创)FragmentTabHost的应用(fragment学习系列文章之二) 时间 2014-04-14 00:11:46 CSDN博客 原文 http://blog.csdn.net/flyi ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
随机推荐
- 谈谈一些有趣的CSS题目(三)-- 层叠顺序与堆栈上下文知多少
开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...
- JS正则表达式常用总结
正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- Android性能优化之利用LeakCanary检测内存泄漏及解决办法
前言: 最近公司C轮融资成功了,移动团队准备扩大一下,需要招聘Android开发工程师,陆陆续续面试了几位Android应聘者,面试过程中聊到性能优化中如何避免内存泄漏问题时,很少有人全面的回答上来. ...
- pt-mext
pt-mext实现的功能比较简单,就是将mysqladmin输出的多次迭代的相同status变量值放到同一行输出. 参数很少,除了--help和--version外,只有一个--relative参数 ...
- Mac OS 使用 Vagrant 管理虚拟机(VirtualBox)
Vagrant(官网.github)是一款构建虚拟开发环境的工具,支持 Window,Linux,Mac OS,Vagrant 中的 Boxes 概念类似于 Docker(实质是不同的),你可以把它看 ...
- ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件
作者: Rick Anderson 翻译: 娄宇(Lyrics) 校对: 高嵩 章节: 介绍视图组件 创建视图组件 调用视图组件 演练:创建一个简单的视图组件 附加的资源 查看或下载示例代码 介绍视图 ...
- 微软发布VSBT,无需安装Visual Studio即可实现项目编译
安装了Visual Studio的那些使用微软平台的开发者通常能够非常容易地操作自己的项目:打开解决方案,修改内容,设置好所有必须的文件以及配置后编译项目.但是在构建服务器或者持续交付系统等没有安装V ...
- Flex 布局教程:语法篇
作者: 阮一峰 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便 ...
- RMS去除在线认证
在微软 OS 平台创建打开 RMS 文档如何避免时延 相信我们在企业内部的环境中已经部署了微软最新的OS平台,Windows 7和Windows 2008 R2,在这些OS平台上使用IRM功能时,您有 ...