android 选项卡TabHost
选项卡主要有TabHost、TabWiget和 FramentLayout3个组件组成,用于实现一个多标签的用户界面,通过他可以将一个复杂的对话分隔成若干个标签页,实现对信息的分类显示和管理。使用给组件不仅可以使界面美观大方,还可以有效地减少窗体个数。
在Android中,实现选项卡的一半步骤如下:
(1)在布局文件中添加实现选项卡所需的TabHost、TabWiget和 FramentLayout组件。
(2)编写各个标签页中要显示内容所对应的XML布局文件。
(3)在Activity个,获取并初始化TabHost组件。
(4)为TabHost对象添加标签页。
1.主布局文件:
activity_main.layout.xml:
- <TabHost 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
- android:id="@android:id/tabhost">
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TabWidget
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@android:id/tabs"
- android:layout_alignParentBottom="true"></TabWidget>
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@android:id/tabcontent"></FrameLayout>
- </RelativeLayout>
- </TabHost>
2.编写各标签页要显示内容对应的XML布局文件。
tab1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:orientation="vertical"
- android:id="@+id/linearLayout01">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="我是Tab1"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/img01"/>
- </LinearLayout>
tab2.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:orientation="vertical"
- android:id="@+id/linearLayout02">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="我是Tab2"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/img02"/>
- </LinearLayout>
MainActivity.java
- public class MainActivity extends ActionBarActivity {
- private TabHost tabHost=null;
- private TabWidget tabWidget=null;
- private FrameLayout frameLayout=null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tabHost= (TabHost) findViewById(android.R.id.tabhost); //声明TabHost组件对象
- tabHost.setup(); //初始化TabHost组件
- //为TabHost对象添加标签页。
- LayoutInflater inflater=LayoutInflater.from(this); //申明LayoutInflater对象
- inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
- inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
- tabHost.addTab(tabHost.newTabSpec("未接电话")
- .setIndicator("未接电话")
- .setContent(R.id.linearLayout01)); //添加第一个标签页
- tabHost.addTab(tabHost.newTabSpec("已接电话")
- .setIndicator("已接电话",getResources().getDrawable(R.drawable.icon))
- .setContent(R.id.linearLayout02)); //添加第二个标签页
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
setIndicator()可以设置选项卡得图标和文字。
需要注意几点是:
TabHost必须为 android:id="@android:id/tabhost"
,
TabWidget 必须为 android:id="@android:id/tabs" ,
FrameLayout android:id="@android:id/tabcontent" ;
效果图(更改TabWiget位置可以实现让TabWiget显示在顶端或低端):
android 选项卡TabHost的更多相关文章
- Android选项卡TabHost方式实现
1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...
- Android选项卡TabHost功能和用法
1.布局文件 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- Android 自学之选项卡TabHost
选项卡(TabHost)是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组建摆放区域.通过这种方式,就可以在一个容器中放置更多组件 ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
- Android零基础入门第63节:过时但仍值得学习的选项卡TabHost
原文:Android零基础入门第63节:过时但仍值得学习的选项卡TabHost 由于前几天参加一个学习培训活动,几乎每天都要从早晨7点到晚上一两点,没有什么时间来分享,实在抱歉中间断更了几天.从今天开 ...
- android自定义tabhost,tabcontent用intent获得
地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...
- 浅谈Android选项卡(二)
前面简单介绍了选项卡,下面以及后面的几篇文章介绍下Android选项卡的几种简单实现方法. http://blog.csdn.net/xia215266092/article/details/9613 ...
- android中TabHost和RadioGroup
android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton 在android中实现菜单功能有多种方法. Options Menu:用户 ...
- Android选项卡学习
什么是选项卡 顶部的导航条就是选项卡了. Android开发中添加选项卡的步骤 图片不太懂上代码: activity_main.xml <?xml version="1.0" ...
随机推荐
- [JLOI2015]城池攻占
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- CodeForces - 724G:Xor-matic Number of the Graph
两点之间的任意路径都可表示为 随便某一条路径xor任何多个环, 然后可以用线性基来做,这样不会重复的, 另外必须一位一位的处理,xor是不满足结合律的 #include<cstdio> ...
- 洛谷P1856 [USACO5.5]矩形周长Picture
题目背景 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 题目描述 编写一个程序计算周长. 如图1所示7个矩形. ...
- hdu 5266 pog loves szh III(lca + 线段树)
I - pog loves szh III Time Limit:6000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I ...
- 2015 多校联赛 ——HDU5349(水)
Problem Description A simple problem Problem Description You have a multiple set,and now there are t ...
- hdu 5012(bfs)
题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...
- Bubble Cup X - Finals [Online Mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...
- HNOI2002 营业额统计(Splay Tree)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意: Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并 ...
- [IOI1998] Pictures
用线段树维护区间最小值和最小值个数来求一段区间里0的个数,把横的和竖的边分别拿出来,排序,然后每次查一下重复部分的长度即可 #include<iostream> #include<c ...
- MVC简单随笔
MVC的具体含义是:model+view+controller,即模型+视图+控制它们各自处理自己的任务: (1)模型(model):模型持有所有的数据.状态和程序逻辑.模型独立于视图和控制器.(2) ...