开源项目AndroidUtil-采用Fragment实现TabHost
原文出自:方杰|http://fangjie.info/?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后的代码,有几点区别:
- 3.0之前的应该导入 import android.support.v4.app.*; 这个包中Fragment相关类;3.o之后的可以直接导入android.app.*;
- 3.0之前的MainAcitivity要继承自FragmentActivity,3.0之后的直接继承自Activity;
- 3.0之前 fragmentManager = getSupportFragmentManager();3.0之后 fragmentManager = getFragmentManager();
开源项目AndroidUtil-采用Fragment实现TabHost的更多相关文章
- 开源项目AndroidUtil-採用Fragment实现TabHost
原文出自:方杰|http://fangjie.sinaapp.com/?p=141 转载请注明出处 学习Android也有一段时间了.感觉大部分的Android应用都有非常多类似的组件,所以就打算做了 ...
- 原创开源项目HierarchyViewer for iOS 2.1 Beta新功能介绍
回顾 HierarchyViewer for iOS是我们发布的一个开源项目,采用GPL v3.0协议. HierarchyViewer for iOS可以帮助iOS应用的开发和测试人员,在没有源代码 ...
- 开源项目月刊《HelloGitHub》第 60 期
兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...
- Diycode开源项目 搭建可以具有下拉刷新和上拉加载的Fragment
1.效果预览 1.1.这个首页就是一个Fragment碎片,本文讲述的就是这个碎片的搭建方式. 下拉会有一个旋转的刷新圈,上拉会刷新数据. 1.2.整体结构 首先底层的是BaseFragment 然后 ...
- Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console、file等。Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别。
Log4J是Apache组织的开源一个开源项目,通过Log4J,可以指定日志信息输出的目的地,如console.file等.Log4J采用日志级别机制,请按照输出级别由低到高的顺序写出日志输出级别. ...
- Fragment为载体可自己主动布局的CardView(GitHub上写开源项目初体验)
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 前些天一直在看Android5.0 的Material Desgin,里面新增 ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
- GitHub上史上最全的Android开源项目分类汇总 (转)
GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...
- Android开源项目汇总【转】
主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...
随机推荐
- iOS 短信验证码倒计时按钮的实现
验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的小伙伴们可以参考一下: 实现思路: 创建按钮,添加点击方法: 用NSTimer定时器, ...
- java多态---内存关系
在该列中,a.lookDoor()会报错,因为azhong没有lookDoor这个方法,同理,a.playGame()也会报错. 注意!!! 最后一句Dog dd=(Dog)a: 这句话非常错误! 在 ...
- linux(centos)上配置nginx、mysql、php-fpm、redis开机启动<转>
原文 http://levi.cg.am/archives/2925 I.nginx开机启动 在/etc/init.d/目录下创建脚本 1 vi /etc/init.d/nginx 更改脚本权限 1 ...
- yii 载入css or js
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . "/js/TableView.js&q ...
- Aircrack-ng 工具箱
官网为:http://www.aircrack-ng.org/, 它就是一个与WiFi 相关的工具啦,可以进行一些注入,抓包.破解WiFI等.里面有很多不同的套件. 另外,http://blog.cs ...
- android studio上代码编译调试中遇到的一些异常记录
下面是记录的在平时代码编写或编译时的一些异常,答案有自己摸索出来的,也有参考其他程序猿朋友的,参考文章过多,就不一一贴出来了. ① E/JavaBinder: !!! FAILED BINDER TR ...
- java之JDBC
java之JDBC 一.什么是JDBC Java 数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提 ...
- 触发TreeView的TreeNodeCheckChanged事件
这个事件不会主动postback,需要手动写javascript触发.对网上找到的方法做了些改进,增加UpdatePanel,以免页面不停的刷.这里就不考虑性能神马的了,因为既然项目已经允许选择使用T ...
- My.Ioc 代码示例——Lifetime 和 ILifetimeScope
很多 Ioc 框架在创建对象的过程中,都会采取某种方式来缓存/复用/释放已构建的对象.在 My.Ioc 中,这个目的是通过 Lifetime/ILifetimeScope 来实现的.其中,Lifeti ...
- asp.net Request.ServerVariables[] 读解
获取客户端的IP地址,代码如下: /// <summary> /// 获取客户端IP地址 /// </summary> /// <returns></retu ...