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函数: ...
随机推荐
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- hadoop 2.7.3本地环境运行官方wordcount-基于HDFS
接上篇<hadoop 2.7.3本地环境运行官方wordcount>.继续在本地模式下测试,本次使用hdfs. 2 本地模式使用fs计数wodcount 上面是直接使用的是linux的文件 ...
- 使用github远程仓库
经过几天对github的研究,终于把自己想完成的给解决了,发现google真的有很多解释,但是很多也会出现一些bug,对于初学者真的很多烦恼,所以整理一份,能给初识github的你有所帮助 一,首先, ...
- NSURLSession详解
导语 现在NSURLConnection在开发中会使用的越来越少,iOS9已经将NSURLConnection废弃,现在最低版本一般适配iOS7,所以也可以使用. NSURLConnection相对于 ...
- mysql-5.6.34 Installation from Source code
Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...
- CentOS:ECDSA host key "ip地址" for has changed and you have requested strict checking(转)
原文地址:http://blog.csdn.net/ausboyue/article/details/52775281 Linux SSH命令错误:ECDSA host key "ip地址& ...
- ubuntu-14.04-server配置Jexus --安装步骤记录
作者:郝喜路 个人主页:http://www.cnicode.com 博客地址:http://haoxilu.cnblogs.com 说明:我是Linux菜鸟,自己尝试配置Jexus服务 ...
- 《高性能javascript》一书要点和延伸(上)
前些天收到了HTML5中国送来的<高性能javascript>一书,便打算将其做为假期消遣,顺便也写篇文章记录下书中一些要点. 个人觉得本书很值得中低级别的前端朋友阅读,会有很多意想不到的 ...
- 电商系统中的商品模型的分析与设计—续
前言 在<电商系统中的商品模型的分析与设计>中,对电商系统商品模型有一个粗浅的描述,后来有博友对货品和商品的区别以及属性有一些疑问.我也对此做一些研究,再次简单的对商品模型做一个介 ...
- Go语言实战 - 使用SendCloud群发邮件
山坡网需要能够每周给注册用户发送一封名为"本周最热书籍"的邮件,而之前一直使用的腾讯企业邮箱罢工了,提示说发送请求太多太密集. 一番寻找之后发现了大家口碑不错的搜狐SendClou ...