12.Android之Tabhost组件学习
TabHost是整个Tab的容器,TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
1)继承TabActivity
如果加载该TabHost画面的类继承TabActivity,并且想通过getTabHost()方法来获取TabHost,getTabWidget()方法获取TabWidget,那么TabHost、TabWidget 、FrameLayout 三者的ID必须是android.R.id.tabhost、android.R.id.tabs、android.R.id.tabcontent, 否则会报运行时异常, 比如提示Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'之类错误
修改主布局activity_main文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back"> <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </LinearLayout> </TabHost>
再增加自己定义的子TabHost页面布局文件tabhost1如下:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小明"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小东"/> </LinearLayout> <LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小红"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小紫"/> </LinearLayout> <LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小李"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小北"/> </LinearLayout> </TabHost>
最后修改下MainActivity.java代码:
package com.example.administrator.dialog1; import android.os.Bundle;
import android.app.TabActivity;
import android.view.LayoutInflater;
import android.widget.TabHost; public class MainActivity extends TabActivity { TabHost mtabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //调用TabActivity的getTabHost()方法获取TabHost对象
mtabHost = this.getTabHost(); //设置使用TabHost布局
LayoutInflater.from(this).inflate(R.layout.tabhost1, mtabHost.getTabContentView(),true); //添加第一个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01)); //添加第二个标签页,并在其标签上添加一个图片
mtabHost.addTab(mtabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab02)); //添加第三个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03)); } }
运行效果:
2) 不继承TabActivity
修改MainActivity.java代码:
package com.example.administrator.dialog1; import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabWidget; public class MainActivity extends Activity { TabHost mtabHost = null;
TabWidget tabWidget = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost1); //以下三句代码,注意顺序
mtabHost = (TabHost)findViewById(R.id.mytabhost);
mtabHost.setup();
tabWidget = mtabHost.getTabWidget(); //添加第一个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01)); //添加第二个标签页,并在其标签上添加一个图片
mtabHost.addTab(mtabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab02)); //添加第三个标签页
mtabHost.addTab(mtabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03)); mtabHost.setCurrentTab(0);
} }
再修改下tabhost1布局文件:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mytabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:id="@+id/tab01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="小明"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:text="小东"/> </LinearLayout> <LinearLayout
android:id="@+id/tab02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="60dp"
android:text="小红"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="60dp"
android:text="小紫"/> </LinearLayout> <LinearLayout
android:id="@+id/tab03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginTop="60dp"
android:text="小李"/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginBottom="60dp"
android:text="小北"/> </LinearLayout> </FrameLayout> </TabHost>
最终运行效果:
12.Android之Tabhost组件学习的更多相关文章
- Android的TabHost组件-android的学习之旅(四十)
TabHost简介 虽然,官方建议用Fagment取代TabHost,但是我们还是大概的介绍一下.TabHost是一种非常简单的组件,TabHost可以很方便的在窗口放置多个标签页,每一个标签页相当于 ...
- Android之EditText组件学习
一.基础学习 1.Button是TextView的一个子类,所以按钮本身是一个特殊的文本,属性和TextView相似 2.EditText类似html里的input type="text&q ...
- Android之TextView组件学习
一.基础学习 1.findViewById返回View类,该类是所有View组件的父类. 2.子类比父类拥有更多的属性和方法,不过子类找不到的话去父类找 3.marquee:华盖,跑马灯效果:orie ...
- Android的四大组件学习
一.Linearlayout : 线性布局 1. android:orientation="vertical" //控件的方向控制,vertical : 垂直布局 , ...
- 从零開始学android<TabHost标签组件.二十九.>
TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...
- Android应用程序组件Content Provider简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6946067 在Android系统中,Conte ...
- Android之TabHost布局(转)
1.概念 盛放Tab的容器就是TabHost.TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab ...
- 转-TabHost组件(一)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...
- 转-TabHost组件(二)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3975095.html 上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定 ...
随机推荐
- UVA 11573 Ocean Currents --BFS+优先队列
采用优先队列做BFS搜索,d[][]数组记录当前点到源点的距离,每次出队时选此时eng最小的出队,能保证最先到达的是eng最小的.而且后来用普通队列试了一下,超时..所以,能用优先队列的,就要用优先队 ...
- 三维网格去噪算法(bilateral filter)
受图像双边滤波算法的启发,[Fleishman et al. 2003]和[Jones et al. 2003]分别提出了利用双边滤波算法对噪声网格进行光顺去噪的算法,两篇文章都被收录于当年的SIGG ...
- .project sturcture和Project Structure 无论是按快捷键或者是从files中打开都不显示
project sturcture和Project Structure 无论是按快捷键或者是从files中打开都不显示 event log中报:IllegalArgumentException:Mul ...
- Android 界面排版的5种方式
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对 ...
- 数字转换为壹仟贰佰叁拾肆的Java方法
网银转帐时, 填写金额后下方出现的汉字金额, 这是Java下的实现. public static String toRMB(double money) { char[] s1 = {'零', '壹', ...
- Android动画原理分析
最近在Android上做了一些动画效果,网上查了一些资料,有各种各样的使用方式,于是乘热打铁,想具体分析一下动画是如何实现的,Animation, Animator都有哪些区别等等. 首先说Anima ...
- 加密算法使用(五):RSA使用全过程
RSA是一种非对称加密算法,适应RSA前先生成一对公钥和私钥. 使用公钥加密的数据可以用私钥解密,同样私钥加密的数据也可以用公钥解密, 不同之处在于,私钥加密数据的同事还可以生成一组签名,签名是用来验 ...
- 史上最全Html与CSS布局技巧
单列布局水平居中水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的父 ...
- 未能进入中断模式,原因如下:源文件“XXXXXX”不属于正在调试的项目。
这个问题是由于项目文件位置变动导致的.提示框已经说的比较清楚了. 首先可以尝试[重新生成] ,一般可以解决这个问题了. 我遇到的情况是,设置配置时,不小心取消了生成选择. 所以打开配置管理器,把相关的 ...
- Photoshop Cc高级设计师培训视频教程(共109节课程)
Photoshop Cc高级设计师培训视频教程(共109节课程) 专业培训Photoshop技能,其他网站收费内容,这里收集了,免费分享给你们哦~ 以下为部分截图: 下载地址: http://fu83 ...