菜鸟学Android之TabHost(一)
来源:互联网 发布:逆战挂机软件 编辑:程序博客网 时间:2024/05/10 22:28
TabHost是一种很有用的组件,可以很方便的在窗口下放置多个标签页,每个标签页相当于获得了一个与外部容器同等大小的组件摆放区域。通过这种方式,就可以在一个容器里放置更多组件,例如手机系统在同一个窗口定义多个标签页,来显示通话记录,包括“未接电话”、“已接电话”、”播出电话”等。
记录一下我学习创建TabHost的过程:
一.继承Activity,创建最基本的tabhost
- 创建好工程后,首先写布局文件,内容如下:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TabHost android:id = "@+id/mytabhost" android:layout_width = "match_parent" android:layout_height = "wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TabWidget android:id = "@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id = "@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content"> </FrameLayout> </LinearLayout> </TabHost></RelativeLayout>
在这一块,要注意TabWidget和FrameLayout的id:TabWidget的id必须定义为android:id = "@android:id/tabs”,FrameLayout的id必须定义为android:id = "@android:id/tabcontent"。其中TabWidget中定义了各个tab间的布局关系,FramLayout中定义了每个tab的布局。为了使得每个tab表示的内容更加丰富,我选择在其他布局文件中为每个tab设置布局,所以此处FrameLayout写的比较简单。 - 写好布局文件后,需要为每个tab配置相应的布局文件,此处为了简单,每个布局文件中,只有一个TextView,来表明这是第几个tab,共有3个tab。以一个为例:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id = "@+id/tab1"> <TextView android:id = "@+id/view1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一个tab" /></RelativeLayout>
- 接下来就要写java文件啦~
先来看一下效果图~
比较简陋,后期加工~
代码附上:package com.young.tabdemo;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.TabHost;public class MainActivity extends Activity {private TabHost tabhost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabhost = (TabHost)findViewById(R.id.mytabhost);tabhost.setup();LayoutInflater layoutInflater = LayoutInflater.from(this);layoutInflater.inflate(R.layout.first, tabhost.getTabContentView()); tabhost.addTab(tabhost.newTabSpec("tab01") .setIndicator("第一个").setContent(R.id.tab1)); layoutInflater.inflate(R.layout.second, tabhost.getTabContentView()); tabhost.addTab(tabhost.newTabSpec("tab02") .setIndicator("第二个").setContent(R.id.tab2)); layoutInflater.inflate(R.layout.third, tabhost.getTabContentView()); tabhost.addTab(tabhost.newTabSpec("tab03") .setIndicator("第三个").setContent(R.id.tab3)); tabhost.setCurrentTab(0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
这是最简单的TabHost实现,tabhost要先setup,然后核心语句来了,这也是困扰我比较长时间的地方:
第一步,LayoutInflater layoutInflater = LayoutInflater.from(this);
声明一个LayoutInfalter对象。
第二步,layoutInflater.inflate(R.layout.first, tabhost.getTabContentView());
调用该对象的inflater方法,将xml文件实例化
第三步,tabhost.addTab(tabhost.newTabSpec("tab01").setIndicator("第一个").setContent(R.id.tab1));
向tabhost中添加tab,调用的TabHost.addTab函数。但我们还没有tab,所以在addTab函数参数中新建了tab,调用的newTabSpec函数,并通过setIndicator函数设置该tab上显示的内容,通过setContent(int viewId)函数设置该tab下对应的布局文件。除此之外,也可以调用setContent(Intent intent)来设置tab内容,这就意味着TabHost.newTabSpec可直接装载一个activity。即下一步所做。
特别注意,在添加每一个tab前都要对相应布局文件调用LayoutInflater.inflate()函数。由于之前对LayoutInflater比较陌生,特意查了些资料,了解到他的作用就相当于findViewById,只不过他是把xml文件实例化,并可以通过LayoutInflater.inflate()来装入或动态装入界面。具体可以参见http://lpqsun-126-com.iteye.com/blog/1158070 - 接下来,为了让每个标签页显示的内容更丰富,试着将每个标签页下装在的xml文件,换成activity。
每个标签页对应的activity和平时的activity一样。核心代码变化比较大,上面粗体部分代码变为:LocalActivityManager localActivityManager = new LocalActivityManager(this,false);localActivityManager.dispatchCreate(savedInstanceState);tabhost.setup(localActivityManager);tabhost.addTab(tabhost.newTabSpec("tab01") .setIndicator("第一个").setContent(new Intent(this,FirstTab.class)));tabhost.addTab(tabhost.newTabSpec("tab02") .setIndicator("第二个").setContent(new Intent(this,SecondTab.class)));tabhost.addTab(tabhost.newTabSpec("tab03") .setIndicator("第三个").setContent(new Intent(this,ThirdTab.class)));
上三行还没搞懂,明天继续~LocalActivityManager现在貌似已经弃用了~
后三行,可以看出,变化主要在调用的setContent方法的参数是Intent对象。
需要特别注意的一点,我经常会忘记的:所有activity都需要再AndroidManifest.xml文件中注册。界面与之前是一样的,不再展示。
二.继承TabActivity,创建基本的TabHost
public class MainActivity extends TabActivity{private TabHost tabhost;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tabhost = getTabHost();tabhost.addTab(tabhost.newTabSpec("tab01") .setIndicator("第一个") .setContent(new Intent(this,FirstTab.class)));tabhost.addTab(tabhost.newTabSpec("tab02") .setIndicator("第二个") .setContent(new Intent(this,SecondTab.class)));tabhost.addTab(tabhost.newTabSpec("tab03") .setIndicator("第三个") .setContent(new Intent(this,ThirdTab.class)));}}感觉还是方便了不少的,起码代码少了好多。tabhost也不需要setup之类的,因为在TabActivity中都已经做好了。不过TabActivity已经不建议用了,详见http://tech.ddvip.com/2013-07/1374006507199157.html前部分介绍
需要注意的有一个地方,xml文件中TabHost的id是规定好了的:android:id = "@android:id/tabhost"。贴出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" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TabHost android:id = "@android:id/tabhost" android:layout_width = "match_parent" android:layout_height = "wrap_content" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TabWidget android:id = "@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id = "@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="wrap_content">" </FrameLayout> </LinearLayout> </TabHost></RelativeLayout>
三.美化
- 选项卡位于底部
其实很简单,将TabWidget和FrameLayout位置互换,FrameLayout中Layout_Weight设为1。
代码:<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" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TabHost android:id = "@+id/mytabhost" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id = "@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.0"> </FrameLayout> <TabWidget android:id = "@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.0" /> </LinearLayout> </TabHost></RelativeLayout>
一开始特别粗心把RealativeLayout和LinearLayout的android:layout_height设置为wrap_content,使得整个选项卡集中在上边,丢人呀~
上下效果图:
比较挫。。。再完善 - 全屏
在activity相应的onCreate方法中,添加两句话:requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
需要注意的是,requestWindosFeature方法必须在setContentView方法之前。
效果图: - 选项卡添加图片
tabhost.addTab(tabhost.newTabSpec("tab01") .setIndicator("通话记录",getResources().getDrawable(R.drawable.records)) .setContent(new Intent(this,FirstTab.class)));tabhost.addTab(tabhost.newTabSpec("tab02") .setIndicator("联系人",getResources().getDrawable(R.drawable.contact)) .setContent(new Intent(this,SecondTab.class)));tabhost.addTab(tabhost.newTabSpec("tab03") .setIndicator("群组",getResources() .getDrawable(R.drawable.groups)) .setContent(new Intent(this,ThirdTab.class)));
- 图片太挫,先不上了。
刚开始学,做出来的东西还是很挫的,继续学习!
参考资料:
http://blog.csdn.net/u012426197/article/details/12678067
http://blog.csdn.net/zingck/article/details/7454316
http://lpqsun-126-com.iteye.com/blog/1158070
http://bbs.9ria.com/thread-229950-1-1.html
http://tech.ddvip.com/2013-07/1374006507199157.html
- 菜鸟学Android之TabHost(一)
- Android 沉淀之TabHost 【一】
- 菜鸟学Android之单元测试
- 菜鸟学Android之简介
- 菜鸟学jsp(一)
- 菜鸟学jsp(一)
- 菜鸟学Python(一)
- 菜鸟学JDBC(一)
- Android入门之TabHost一(不用xml,纯java)
- 菜鸟的Android之旅(一)
- 一起学android之底部菜单TabHost的实现(9)
- 学习Android开发-乐学成语之TabHost控件
- 菜鸟学Android 之 selector背景选择器
- 菜鸟学Android 之 标题栏美化
- 菜鸟学android之ArrayAdapter与Spinner
- 小菜鸟学 MQ(一)
- 菜鸟学PetShop一
- Android之Tab分页标签的实现方法一-----TabActivity和TabHost的结合(一)
- Linux system()函数返回值详解(3)
- Linux ls命令解析
- mysql Key_buffer_size参数的设置
- Gradle在大型Java项目上的应用
- NOSQL之【REDIS】学习(二):配置说明
- 菜鸟学Android之TabHost(一)
- 面向 Java 开发人员的 Scala 指南: 构建计算器,第 1 部分
- IOS APP如何实现升级
- MYSQL中MY.CNF配置文件及参数介绍
- python_20131014
- Django, csrf
- 网页设计技巧内容分享:优化网页加载速度
- html上下框架(demo)
- 哥哥最近有项目没有做完,考试也没有过,吐槽一把