原文出自:方杰|http://fangjie.sinaapp.com/?p=141 转载请注明出处

学习Android也有一段时间了。感觉大部分的Android应用都有非常多类似的组件,所以就打算做了这样一个开源项目,目的是整合一些Android开发经常使用的组件Demo,方便以后项目中直接拿来用。git地址:https://github.com/JayFang1993/AndroidUtil

废话不多说,首先讲第一个经常使用的组件TabHost的实现。之前我们能够通过继承TabActivity来实现,后来的API中已经不建议用这样的方式了,所以今天我们主要讲的是用Fragment来实现Tabhost。

在新浪微博等非常多APP中都有底部选项卡TabHost。

首先看下实现后的效果。

一、TabHost的实现

Tabhost的每个选项卡是通过RadioGroup实现的,每个Tab就是一个RadioButton。

页面除TabHost以外的内容区域是Fragment。

以下是详细的布局文件main.xml

<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=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:id="@+id/title"
android:text="昌大软院"
android:textSize="18dp"
android:textColor="#a8aeb5"
android:typeface="monospace"
android:background="@drawable/title_bg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"
android:textColor="#a8aeb5"
android:layout_marginTop="10dp"
android:layout_marginRight="8dp"
android:background="@drawable/done" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back"
android:textColor="#a8aeb5"
android:text="Back"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="8dp"
/> <FrameLayout
android:id="@+id/content"
android:layout_below="@id/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/> <RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="@drawable/tabhost_bg" >
<RadioButton
android:id="@+id/rb_home"
android:drawableTop="@drawable/tab1"
style="@style/tab"
android:text="主页" />
<RadioButton
android:id="@+id/rb_at"
style="@style/tab"
android:drawableTop="@drawable/tab2"
android:text="收藏夹" />
<RadioButton
android:id="@+id/rb_mess"
style="@style/tab"
android:drawableTop="@drawable/tab3"
android:text="我" />
<RadioButton
android:id="@+id/rb_more"
style="@style/tab"
android:drawableTop="@drawable/tab4"
android:text="很多其它" />
</RadioGroup>
</RelativeLayout>

每个Tab的样式:宽度、高度、背景图片这些都是同样的。所以写在了一个style文件里。styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="tab">
<item name="android:layout_height">48dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:textSize">10dp</item>
<item name="android:paddingTop">8dp</item>
<item name="android:background">@drawable/tabhost_bg_selector</item>
<item name="android:textColor">#a8aeb5</item>
<item name="android:button">@null</item>
</style>
</resources>

为了可以制造出Tab按下选中的效果,所以为Tab的背景设计了一个selector。tabhost_bg_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tabhost_press" android:state_pressed="true" />
<item android:drawable="@drawable/tabhost_press" android:state_checked="true" />
<item android:drawable="@drawable/tabhost_bg"/>
</selector>

至此,关于TabHost的全部布局文件都写完了。

以下看看Activity中的Java代码。

MainActivity.java

public class MainActivity extends FragmentActivity {
private FragmentManager fragmentManager;
private RadioGroup radioGroup;
private RadioButton rb1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
radioGroup = (RadioGroup) findViewById(R.id.main_radio);
rb1=(RadioButton) findViewById(R.id.rb_home);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@SuppressLint("NewApi")
public void onCheckedChanged(RadioGroup group, int checkedId) {
rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tabhost_bg_selector));
FragmentTransaction transaction = fragmentManager.beginTransaction();
ContentFrame fragment = null;
switch(checkedId)
{
case 0:
fragment= new ContentFrame();
break;
case 1:
fragment= new ContentFrame();
break;
case 2:
fragment= new ContentFrame();
break;
case 3:
fragment= new ContentFrame();
break;
default:
fragment= new ContentFrame();
break;
}
transaction.replace(R.id.content, (Fragment)fragment);
transaction.commit();
}
});
//设置默认选中第一项
radioGroup.check(1);
radioGroup.check(0);
//设置按下的背景效果
rb1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tabhost_press));
}
}

针对每个选项卡的内容界面代码,随便写一个布局文件content.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
/>
</LinearLayout>

内容部分的Java代码,实现和Activity几乎相同。只是这里须要继承Fragment而不是Activity。

从content.xml中得到一个View。然后将这个View替换Main中的Fragment部分。

public class ContentFrame extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content, null);
TextView textView = (TextView) view.findViewById(R.id.content);
textView.setText("Hello world");
return view;
}
}

 补充:以上代码是考虑到Android 3.0曾经API中没有Fragment。导入android-support-v4.jar后的代码,有几点差别:

  1. 3.0之前的应该导入  import android.support.v4.app.*; 这个包中Fragment相关类;3.o之后的能够直接导入android.app.*;
  2. 3.0之前的MainAcitivity要继承自FragmentActivity。3.0之后的直接继承自Activity;
  3. 3.0之前   fragmentManager = getSupportFragmentManager();3.0之后  fragmentManager = getFragmentManager();
 欢迎各位关注我的个人网站:http://fangjie.sinaapp.com/ 

开源项目AndroidUtil-採用Fragment实现TabHost的更多相关文章

  1. 开源项目AndroidUtil-采用Fragment实现TabHost

    原文出自:方杰|http://fangjie.info/?p=141 转载请注明出处 学习Android也有一段时间了,感觉大部分的Android应用都有很多类似的组件,所以就打算做了这样一个开源项目 ...

  2. Diycode开源项目 搭建可以具有下拉刷新和上拉加载的Fragment

    1.效果预览 1.1.这个首页就是一个Fragment碎片,本文讲述的就是这个碎片的搭建方式. 下拉会有一个旋转的刷新圈,上拉会刷新数据. 1.2.整体结构 首先底层的是BaseFragment 然后 ...

  3. Fragment为载体可自己主动布局的CardView(GitHub上写开源项目初体验)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 前些天一直在看Android5.0 的Material Desgin,里面新增 ...

  4. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

  5. 59.Android开源项目及库 (转)

    转载 : https://github.com/Tim9Liu9/TimLiu-Android?hmsr=toutiao.io&utm_medium=toutiao.io&utm_so ...

  6. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

  7. Android开源项目汇总【转】

    主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...

  8. GitHub上史上最全的Android开源项目分类汇总

    今天在看博客的时候,无意中发现了 @Trinea 在GitHub上的一个项目 Android开源项目分类汇总 ,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫 ...

  9. Android 开源项目分类汇总(转)

    Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...

随机推荐

  1. leetCode(28):Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  2. 分享一个基于 Node.js 的 Web 开发框架 - Nokitjs

    简介 Nokit 是一个简单易用的基于 Nodejs 的 Web 开发框架,默认提供了 MVC / NSP / RESTful 等支持,并提供对应项目模板.管理工具. 资源 GitHub https: ...

  3. Python学习(七)面向对象 ——类和实例

    Python 面向对象 —— 类和实例 类 虽然 Python 是解释性语言,但是它是面向对象的,能够进行对象编程.至于何为面向对象,在此就不详说了.面向对象程序设计本身就很值得深入学习,如要了解,请 ...

  4. iOS开源项目:JSONKit

    一个Json解析库,其特点是代码简单,只有一个.h和.m文件. https://github.com/johnezang/JSONKit JSON(JavaScript Object Notation ...

  5. Java程序猿笔试面试之String4

    怎样删除String中反复的字符good? 思想一:蛮力法,进行双重循环,此算法的复杂度为O(n^2),n是指字符串的长度 public class RemoveSameChar { public s ...

  6. 数学图形(2.14)Spherical helix曲线

    从http://mathworld.wolfram.com/SphericalHelix.html上找到如下一些关于该曲线的说明,不过似乎他的公式和我的脚本完全是两个东西.. The tangent  ...

  7. 刚子扯谈:网站运营在左 技术在右 真TM扯

    朋友的书 鄙人的书 2013年8月5日,雨未下,天猴焖 开片语:今天的扯谈内容是我转载我Java学习交流群里面一个哥们,当然我推荐他加入了朋友的网络分析师这个群,我认为那样对他有更大帮助.现在他是XX ...

  8. go语言基础之字符串类型 和 字符与字符串类型的区别

    1.字符串类型 示例1: package main //必须有一个main包 import "fmt" func main() { var str1 string str1 = & ...

  9. linux 处理键盘 鼠标事件

    Linux下鼠标和键盘的模拟控制,也就是为手势和语音控制鼠标和键盘部分服务的. 有关于本系统构建的文章结构都会由三个部分来组织,一是该功能模块的介绍和在Linux下简单应用程序的实现:二是将该功能模块 ...

  10. IE、火狐导入收藏夹乱码解决方案

    IE收藏夹导入Firefox书签后出现乱码的原因在于两个浏览器的收藏夹的html文件的编码方式不同,Firefox使用的是UTF-8编码,而IE是ASCII编码.故两者的收藏夹文件互相导入的时候会出现 ...