android组件之TabHost
一 介绍
1.1 TAB的容器。这个对象包含两个子元素:
- TabWidget:管理标签(tabs),用户点击来选择一个特定的标签,是它告诉TabHost去切换界面的
- FrameLayout:对象显示该页的内容
1.2 常用方法
- public void addTab(TabHost.TabSpec tabSpec)
- public void setup ():在addTab之前要先调用setup,不和TabActivity关联,通过findViewById获取的TabHost需要调用setup(),如果是在TabActivity中通过getTabHost()的方式获取的不需要调用这个方法
- setup(LocalActivityManager activityGroup): setContent中传入intent的时候才关注下这个方法
- clearAllTabs :移除所有的tab
- dispatchKeyEvent : 下发keyevent
- dispatchWindowFocuseChanged : 下发windowfocusChanged事件
- tabHost.newTabSpec :创建一个新的TabSpec,关联到具体内容
- getCurrentTab()/setCurrentTab():获取/设置当前需要显示的tab,通过index
- setCurrentTabByTag/getCurrentTabTag:通过tag设置当前需要显示的Tab,tag就是创建TabSpec的时候传入的字符串
- getCurrentTabView:设置/获取当前在TabWidget中显示的View,也就是作为标签的View而非内容
- getCurrentView :获取当前显示的内容
- setOnTabChangedListener : 设置标签页切换事件监听
- getTabContentView:获取内容页面的容器FrameLayout
- getTabWidget: 获取TabWidget
1.3 涉及接口及类
- class:TabHost.TabSpec
- interface:TabHost.OnTabChangeLisetener
- interface:TabHost.TabContentFactory
1.3.1 TabHost.TabSpec
tab(标签)有一个indicator,content.例如:
tabHost.addTab(tabHost.newTabSpec("tab_time").setIndicator("时钟").setContent(R.id.tab_time));
1.3.2 indicator
有三个重载的方法可以设置标签的名字和图案。返回值都是TabHost.TabSpec
- setIndicator(CharSequence label)
- setIndicator(View view)
- setIndicator(CharSequence lable,Drawable icon)
1.3.3 content
返回值都是TabHost.TabSpe。是第一个比较常用。
- **setContent(int viewId)**传入视图的ID与之关联起来
- setContet(Intent intent)在TabHost.TabContentFactory创建的这个视图的内容
- setContent((TabHost.TabContentFactory contentFactory)
1.3.4 tag
这是相当于一个tag的身份证,在 new TabSpec(String tag)决定了
二 TabHost源码
public class TabHost...{
//常用属性
private TabWidget mTabWidget;
private FrameLayout mTabContent;
private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2);
private OnKeyListener mTabKeyListener;
public void setup(){
//这里实例化TabWiget
mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
if (mTabWidget == null) {
throw new RuntimeException(
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'");
}
....
mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
public void onTabSelectionChanged(int tabIndex, boolean clicked) {
setCurrentTab(tabIndex);
if (clicked) {
mTabContent.requestFocus(View.FOCUS_FORWARD);
}
}
});
mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
if (mTabContent == null) {
throw new RuntimeException(
"Your TabHost must have a FrameLayout whose id attribute is "
+ "'android.R.id.tabcontent'");
}
}
}
注意:在自定义自己的TabHost的时候,Tabwiget和FrameLayout不可以自定义Id。为它需要在setup里面实例化,因此需要在addTab添加内容之前调用setup方法
三 实例分析
代码地址:https://github.com/MichealPan9999/TabHost
3.1 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"> <TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 设置的id必须是 "@android:id/tabcontent"-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"> <LinearLayout
android:id="@+id/tv_one"
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:text="this is tab1 content" />
</LinearLayout> <LinearLayout
android:id="@+id/tv_two"
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:text="this is tab2 content" />
</LinearLayout> <LinearLayout
android:id="@+id/tv_three"
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:text="this is tab3 content" />
</LinearLayout> <LinearLayout
android:id="@+id/tv_four"
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:text="this is tab4 content" />
</LinearLayout>
</FrameLayout>
<!-- 设置的id必须是android:id="@android:id/tabs" -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="5dp"
android:showDividers="none"></TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
</LinearLayout>
3.2 MainActivity 实现
public class MainActivity extends FinalActivity {
// 如果没有继承TabActivity时,通过该种方法加载启动tabHost
@ViewInject(id = R.id.tabhost)
TabHost mTabHost;
private int[] unSelectedTabIcons = {R.drawable.menu_icon_0_normal, R.drawable.menu_icon_1_normal, R.drawable.menu_icon_2_normal, R.drawable.menu_icon_3_normal};
private int[] selectedTabIcons = {R.drawable.menu_icon_0_pressed, R.drawable.menu_icon_1_pressed, R.drawable.menu_icon_2_pressed, R.drawable.menu_icon_3_pressed};
private String[] menuText = {"添加联系人", "通讯录", "备忘录","设置"};
private int[] contentItems = {R.id.tv_one, R.id.tv_two, R.id.tv_three,R.id.tv_four};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost.setup();//初始化TabHost,在addTab之前要先调用setup;不和TabActivity关联,通过findViewById获取的TabHost需要调用setup();如果是在TabActivity中通过getTabHost()的方式获取的不需要调用这个方法
addTabHost();
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
updateTab(mTabHost,tabId);
}
});
mTabHost.setCurrentTab(0);
((TextView) mTabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorGreen));//第一次进入的时候讲选中的Tab修改文字颜色
((ImageView) mTabHost.getCurrentTabView().findViewById(R.id.iv_indicator)).setImageResource(selectedTabIcons[0]);//第一次进入的时候讲选中的Tab修改文字颜色
}
private void addTabHost() {
for (int i = 0;i<;i++)
{
View view = LayoutInflater.from(this).inflate(R.layout.indicator, null, false);
TextView textView = (TextView) view.findViewById(R.id.tv_indicator);
ImageView imageView = (ImageView) view.findViewById(R.id.iv_indicator);
textView.setText(menuText[i]);
imageView.setImageResource(unSelectedTabIcons[i]);
mTabHost.addTab(mTabHost.newTabSpec(String.valueOf(i)).setIndicator(view).setContent(contentItems[i]));
}
}
private void updateTab(TabHost tabHost,String tabId) {
for (int i = 0; i < 4; i++) {
((TextView) tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorBlue));
((ImageView) tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.iv_indicator)).setImageResource(unSelectedTabIcons[i]);//第一次进入的时候讲选中的Tab修改文字颜色
}
Log.e("panzqww",tabHost.getCurrentTabTag()+" ::: "+tabId);
if (tabHost.getCurrentTabTag().equals(tabId)) {
int i = Integer.valueOf(tabId);
((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorGreen));
((ImageView) tabHost.getCurrentTabView().findViewById(R.id.iv_indicator)).setImageResource(selectedTabIcons[i]);//第一次进入的时候讲选中的Tab修改文字颜色
}//选中的那个Tab文字颜色修改
}
}
3.3 TabActivity
TabActivity已经被废弃很久了,但是还是可以使用,在布局中将TabHost的id改成android:id=”@android:id/tabhost”,然后在继承了TabActivity的MainActivity.java中使用
mTabHost = getTabHost();
然后基本使用方法就和上面一样了
android组件之TabHost的更多相关文章
- AndroidUI组件之TabHost
package com.gc.tabhost; /** * @author Android将军 * * * * 1.TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置 * 多 ...
- 2015最流行的Android组件、工具、框架大全
Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件. 本文试图将目前流行的组件收集起来以 ...
- Android组件化
附:Android组件化和插件化开发 App组件化与业务拆分那些事 Android项目架构之业务组件化 Android组件化核心之路由实现 Android组件化开发实践
- Android组件安全
今天在看有关Android组件安全的东西 1.Activity Android系统组件在指定Intent过滤器(intent-filter)后,默认是可以被外部程序(签名不同,用户ID不同)访问的,在 ...
- Android组件的通讯——Intent
转载:Android组件的通讯-Intent 1.概述 一个应用程序的三个核心组件——activities.services.broadcast receivers,都是通过叫做intents的消息激 ...
- 使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏
android组件之间的通信有多种实现方式,Broadcast就是其中一种.在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例. 效果如图: 布局 ...
- Android组件系列----BroadcastReceiver广播接收器
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
- 最流行的android组件大全
目录 [−] 工具和教程 UI组件 类库 游戏引擎 Android HTML5应用 Android 是目前最流行的移动操作系统(还需要加之一吗?). 随着新版本的不断发布, Android的功能也日益 ...
- Android组件生命周期(二)
引言 应用程序组件有一个生命周期——一开始Android实例化他们响应意图,直到结束实例被销毁.在这期间,他们有时候处于激活状态,有时候处于非激活状态:对于活动,对用户有时候可见,有时候不可见.组件生 ...
随机推荐
- python判断小数示例&写入文件内容示例
#需求分析: #1.判断小数点个数是否为1 #2.按照小数点分隔,取到小数点左边和右边的值 #3.判断正小数,小数点左边为整数,小数点右边为整数 #4.判断负小数,小数点左边以负号开头,并且只有一个负 ...
- python之读写文件
1. 读取文件数据,文件必须存在才可以读且如要读取的文件不和当前.py在同一个包下,需要特别指定此文件路径才行 f=open('test.txt',encoding='utf-8')#填写文件路径,打 ...
- jstl和EL表达式混合使用
EL表达式里判断字符串,或者... ${(wrt.acceptName eq '刘立荣') || (wrt.acceptName eq '卢伟冰') } <tr> <td heigh ...
- hibernate(一) 第一个hibernate工程
序言 其实hibernate已经学过一遍,不过因为太糊弄,急于求成,导致现在需要重新来学习,通过亲自去敲每一行代码,来去理解每一个知识点. ---WH 一.什么是Hibernate? 轻量级JavaE ...
- 使用WebView时软键盘遮挡H5页面解决办法
简单解决办法:在清单文件中添加 android:windowSoftInputMode="adjustResize" 此举可在软键盘弹出时,重新测量布局,保证不遮挡光标的所在位置. ...
- vue 学习笔记—axios(替代vue-resource)
一.使用 1. 引入CDN的方式 https://unpkg.com/axios@0.16.2/dist/axios.min.js 或者 npm方式 npm install axios --sa ...
- Linux下安装PCRE
原文链接:https://www.linuxidc.com/Linux/2015-03/114986.htm PCRE(Perl Compatible Regular Expressions)是一个轻 ...
- Linux C++ TCP Socket传输文件或图片实例
环境:Linux 语言:C++ 通信方式:TCP 下面用TCP协议编写一个简单的服务器.客户端,其中服务器端一直监听本机的6666号端口.如果收到连接请求,将接收请求并接收客户端发来的消息:客户端与服 ...
- 用多线程处理FTP上传
在开发中遇到总站发送命令请求分站将某资源通过FTP上传过来,也就是总站提取分站的资源问题.并且总站实时可以获取已经提取了文件的大小的比例. 思路:1.首先分站要将文件大小告知总站 2.总站收到文件大小 ...
- 3D中的旋转变换
相比 2D 中的旋转变换,3D 中的旋转变换复杂了很多.关于 2D 空间的旋转,可以看这篇文章.本文主要粗略地探讨一下 3D 空间中的旋转. 旋转的要素 所谓旋转要素就是说,我们只有知道了这些条件,才 ...