android tabhost的使用方法,Android TabHost组件使用方法详解
最近研究了一下contacts源码,仿照上面自己写了一个tabhosttest程序,现整理如下:
main.xml布局文件:
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
inner.xml文件:
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
main.java (主activity类):
package com.android.test;
import android.app.activity;
import android.app.tabactivity;
import android.content.intent;
import android.os.bundle;
import android.provider.calllog.calls;
import android.provider.contacts.intents.ui;
import android.view.window;
import android.widget.tabhost;
public class main extends tabactivity implements tabhost.ontabchangelistener {
private static final int tab_index_dialer = 0;
private static final int tab_index_call_log = 1;
private static final int tab_index_contacts = 2;
private static final int tab_index_favorites = 3;
private tabhost mtabhost;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
final intent intent = getintent();
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.main);
mtabhost = gettabhost();
mtabhost.setontabchangedlistener(this);
// setup the tabs
setupdialertab();
setupcalllogtab();
setupcontactstab();
setupfavoritestab();
setcurrenttab(intent);
}
public void ontabchanged(string tabid) {
activity activity = getlocalactivitymanager().getactivity(tabid);
if (activity != null) {
activity.onwindowfocuschanged(true);
}
}
private void setupcalllogtab() {
// force the class since overriding tab entries doesn't work
intent intent = new intent("com.android.phone.action.recent_calls");
intent.setclass(this, inner.class);
mtabhost.addtab(mtabhost.newtabspec("call_log")
.setindicator("通话记录",
getresources().getdrawable(r.drawable.ic_tab_unselected_recent))
.setcontent(intent));
}
private void setupdialertab() {
intent intent = new intent("com.android.phone.action.touch_dialer");
intent.setclass(this, inner.class);
mtabhost.addtab(mtabhost.newtabspec("dialer")
.setindicator("拨号",
getresources().getdrawable(r.drawable.ic_tab_unselected_dialer))
.setcontent(intent));
}
private void setupcontactstab() {
intent intent = new intent(ui.list_default);
intent.setclass(this, main.class);
mtabhost.addtab(mtabhost.newtabspec("contacts")
.setindicator("通讯录",
getresources().getdrawable(r.drawable.ic_tab_unselected_contacts))
.setcontent(intent));
}
private void setupfavoritestab() {
intent intent = new intent(ui.list_strequent_action);
intent.setclass(this, inner.class);
mtabhost.addtab(mtabhost.newtabspec("favorites")
.setindicator("收藏",
getresources().getdrawable(r.drawable.ic_tab_unselected_starred))
.setcontent(intent));
}
/**
* sets the current tab based on the intent's request type
*
* @param intent intent that contains information about which tab should be selected
*/
private void setcurrenttab(intent intent) {
// dismiss menu provided by any children activities
activity activity = getlocalactivitymanager().
getactivity(mtabhost.getcurrenttabtag());
if (activity != null) {
activity.closeoptionsmenu();
}
// tell the children activities that they should ignore any possible saved
// state and instead reload their state from the parent's intent
intent.putextra("", true);
// choose the tab based on the inbound intent
string componentname = intent.getcomponent().getclassname();
if (getclass().getname().equals(componentname)) {
if (false) {
//in a call, show the dialer tab(which allows going back to the call)
mtabhost.setcurrenttab(tab_index_dialer);
} else if ((intent.getflags() & intent.flag_activity_launched_from_history) != 0) {
// launched from history (long-press home) --> nothing to change
} else if (true) {
// the dialer was explicitly requested
mtabhost.setcurrenttab(tab_index_dialer);
}
}
}
}
inner.java类:
package com.android.test;
import android.app.tabactivity;
import android.content.intent;
import android.os.bundle;
import android.view.window;
import android.widget.tabhost;
import android.widget.tabwidget;
import android.widget.textview;
public class inner extends tabactivity implements tabhost.ontabchangelistener {
private static final int tab_index_all = 0;
private static final int tab_index_missed = 1;
private static final int tab_index_outgoing = 2;
private static final int tab_index_received = 3;
private tabhost mtabhost;
private tabwidget mtabwidget;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.inner);
mtabhost = gettabhost();
mtabhost.setontabchangedlistener(this);
setuptabs();
mtabwidget = mtabhost.gettabwidget();
mtabwidget.setstripenabled(false);
for (int i = 0; i < mtabwidget.getchildcount(); i++) {
textview tv = (textview) mtabwidget.getchildat(i).findviewbyid(
android.r.id.title);
tv.settextcolor(this.getresources().getcolorstatelist(
android.r.color.white));
tv.setpadding(0, 0, 0,(int) tv.gettextsize());
tv.settext("tab" + i);
mtabwidget.getchildat(i).getlayoutparams().height =(int ) (3* tv.gettextsize());
mtabwidget.getchildat(i).setbackgroundresource(r.drawable.tab_bg);
}
}
public void ontabchanged(string tabid) {
}
private void setuptabs() {
mtabhost.addtab(mtabhost.newtabspec("all").setindicator(
getstring(r.string.inner)).setcontent(
new intent(this, other.class)));
mtabhost.addtab(mtabhost.newtabspec("missed").setindicator(
getstring(r.string.inner)).setcontent(
new intent(this, other.class)));
mtabhost.addtab(mtabhost.newtabspec("outgoing").setindicator(
getstring(r.string.inner)).setcontent(
new intent(this, other.class)));
mtabhost.addtab(mtabhost.newtabspec("received").setindicator(
getstring(r.string.inner)).setcontent(
new intent(this, other.class)));
}
}
效果图如下:
以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持萬仟网。
android tabhost的使用方法,Android TabHost组件使用方法详解相关推荐
- Android基础入门教程——2.3.1 TextView(文本框)详解
Android基础入门教程--2.3.1 TextView(文本框)详解 标签(空格分隔): Android基础入门教程 本节引言: 学习完Android中的六大布局,从本节开始我们来一个个讲解And ...
- android相册和拍照并裁剪图片大小,Android 拍照并对照片进行裁剪和压缩实例详解...
Android 拍照并对照片进行裁剪和压缩实例详解 本文主要介绍 Android 调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码. 调用摄像头拍照,对拍摄照片进行裁剪,代码如下. ...
- Android Studio CPU profiler性能分析工具介绍和使用详解
Android Studio CPU profiler性能分析工具介绍和使用详解 CPU profiler介绍 Android Studio CPU 性能剖析器可实时检查应用的 CPU 使用率和线程活 ...
- android自定义圆角进度条,Android自定义进度条的圆角横向进度条实例详解
1.本文将向你介绍自定义进度条的写法,比较简单,但还是有些知识点是需要注意的: invalidate()方法 RectF方法的应用 onMeasure方法的应用 2.原理 画3层圆角矩形,底层为黑色, ...
- Android studio 多渠道(多环境)打包grade配置详解
Android studio 多渠道(多环境)打包grade配置详解 场景:开发app,我们需要两套环境或者两套环境以上的apk,每套环境的apk分两个版本debug版和release版. 公司有套平 ...
- Android Studio打不开虚拟机,两种情况详解
Android Studio打不开虚拟机,两种情况详解 文章目录 Android Studio打不开虚拟机,两种情况详解 1.VT-x is disabled in BIOS 2.Emulator文件 ...
- 组件化实践详解(二)
在上一篇文章<组件化实践详解(一)>中我们介绍了组件化实践的目标和实践步骤,本文继续说说关于组件化实践遇到的问题及思考. 1.组件内的架构设计 这条本来我是不想写的,但是很多组件化的文章里 ...
- Win7下的内置FTP组件的设置详解
Win7下的内置FTP组件的设置详解 在局域网中共享文件,FTP是比较方便的方案之一.Win7内部集成了FTP,只是设置起来颇费一番功夫.着文以记之. 一.安装FTP组件 由于Win7默认没有安装FT ...
- Vue 路由导航守卫(全局守卫、路由独享守卫、组件内守卫)详解
Vue 路由导航守卫(全局守卫.路由独享守卫.组件内守卫)详解: 路由守卫 官方解释 "导航"表示路由正在发生改变.正如其名,vue-router提供的导航守卫主要用来通过跳转或取 ...
- SpringCloud2组件之Feign详解
开发环境: 开发工具:IntelliJ IDEA Java版本:1.8 Spring Boot版本:2.1.6.RELEASE Spring Cloud版本:Greenwich.SR1 工程简介: ...
最新文章
- hibernate c3p0 mysql_hibernate-使用c3p0数据库连接池,以及其它配置
- 腾讯敏捷协作平台TAPD获评2019软博会“优秀产品”
- openoffice 安装windows 环境
- hp服务器装xp系统,教你惠普笔记本一键装xp系统的方法
- 206.12.15随笔--最近内心的一些想法
- 金属100寸“电视”却无屏,语音识别+DTS+1080P,微鲸M1测评
- Tomcat 8默认工具manager管理页面访问配置
- 程序员买房,买车,一个避不开的梗
- 拒绝搜索引擎抓取页面
- MapControl与PageLayoutControl联动
- 宾夕法尼亚大学发明了第一代电子管计算机,新手计算机基础入门
- 连续+离散变量的联合分布求解
- Linux7系统克隆到另一个硬盘,Ubuntu14.04 dd命令克隆系统镜像安装到另一台机器上...
- 萤石云视频监控接入详细流程,添加设备,展示视频,云台控制
- 基于python的opencv的学习
- 台式计算机无法访问网络,台式机关机导致局域网内LAN设备无法连接网络
- FFmpeg编译出来的库太大?试试这几招
- python异常-TypeError: ‘tuple‘ object is not callable.当不同的环境下同一个语句运行结果不同时,不如重启程序、更改不相关变量试试
- 求生之路2服务器消息,求生之路2服务器公告设置
- 变量(什么是变量/变量的声明/命名的规范)