开通博客以来已经约莫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的基本用法的更多相关文章

  1. FragmentTabHost用法

    FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |--- ...

  2. 【Android Widget】FragmentTabHost

    android.support.v4包里面提供了FragmentTabHost用来替代TabHost,FragmentTabHost内容页面支持Fragment,下面我们就通过示例来看他的用法 效果图 ...

  3. FragmentTabHost的应用

    原创)FragmentTabHost的应用(fragment学习系列文章之二) 时间 2014-04-14 00:11:46  CSDN博客 原文  http://blog.csdn.net/flyi ...

  4. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  5. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  6. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  7. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  8. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  9. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

随机推荐

  1. 【Web动画】SVG 线条动画入门

    通常我们说的 Web 动画,包含了三大类. CSS3 动画 javascript 动画(canvas) html 动画(SVG) 个人认为 3 种动画各有优劣,实际应用中根据掌握情况作出取舍,本文讨论 ...

  2. ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件

    作者: Rick Anderson 翻译: 娄宇(Lyrics) 校对: 高嵩 章节: 介绍视图组件 创建视图组件 调用视图组件 演练:创建一个简单的视图组件 附加的资源 查看或下载示例代码 介绍视图 ...

  3. GOF23设计模式归类

    创建型模式:-单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式结构型模式:-适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式行为型模式:-模板方法模式.命令模式.迭代器模式 ...

  4. javascript排序

    利用array中的sort()排序 w3cfunction sortNumber(a,b) { return a - b } var arr = new Array(6) arr[0] = " ...

  5. 品牌营销:不要Beat,要逼格!

             品牌营销:不要Beat,要逼格! 奥美的创始人大卫·奥格威说,广告营销应当是"具有风度的推销产品".而当下的营销手段,"风度"早已被抛之脑后, ...

  6. 参数探测(Parameter Sniffing)影响存储过程执行效率解决方案

    如果SQL query中有参数,SQL Server 会创建一个参数嗅探进程以提高执行性能.该计划通常是最好的并被保存以重复利用.只是偶尔,不会选择最优的执行计划而影响执行效率. SQL Server ...

  7. TCP/IP之TCP_NODELAY与TCP_CORK

    TCP/IP之Nagle算法与40ms延迟提到了Nagle 算法.这样虽然提高了网络吞吐量,但是实时性却降低了,在一些交互性很强的应用程序来说是不允许的,使用TCP_NODELAY选项可以禁止Nagl ...

  8. 搭建个人wordpress博客(小白教程)

    新浪sae平台现在是有个免费个人空间使用,现在,教您如何使用该平台搭建属于自己的个人网站,本教程以wordpress程序安装包搭建个人网站. 申请新浪云账号 如果我们使用SAE新浪云计算平台作为服务器 ...

  9. 【道德经】漫谈实体、对象、DTO及AutoMapper的使用

    写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...

  10. 【腾讯Bugly干货分享】移动客户端中高效使用SQLite

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57b57f2a0703f7d31b9a3932 作者:赵丰 导语 iOS 程序能 ...