最近研究了一下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">

s

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)));

}

}

效果图如下:

androidtabhost缓存_Android TabHost用法详解相关推荐

  1. php theme_path,PHP_Yii2主题(Theme)用法详解,本文实例讲述了Yii2主题(Theme) - phpStudy

    Yii2主题(Theme)用法详解 本文实例讲述了Yii2主题(Theme)用法.分享给大家供大家参考,具体如下: 首先看看主要的配置方式: 'components' => [ 'view' = ...

  2. RxJava flatMap操作符用法详解

    RxJava系列文章目录导读: 一.RxJava create操作符的用法和源码分析 二.RxJava map操作符用法详解 三.RxJava flatMap操作符用法详解 四.RxJava conc ...

  3. python redis用法详解

    使用python来操作redis用法详解 1.1 2017.06.22 16:38* 字数 4875 阅读 96923评论 8喜欢 60 1.redis连接 redis提供两个类Redis和Stric ...

  4. Linux Shell脚本入门--wget 命令用法详解

    Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...

  5. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

  6. csh for循环_shell中的for循环用法详解_linux shell

    这篇文章主要介绍了shell中的for循环用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 for 命令: for i i ...

  7. STL中map和string, vector 用法详解

    1. map 用法详解 std map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成 ...

  8. java设计模式观察者模式吗_Java设计模式之观察者模式原理与用法详解

    Java设计模式之观察者模式原理与用法详解 本文实例讲述了Java设计模式之观察者模式原理与用法.分享给大家供大家参考,具体如下: 什么是观察者模式 可以这么理解: 观察者模式定义了一种一对多的依赖关 ...

  9. linux 内存 参数,linux free命令参数及用法详解(linux查看内存命令)

    linux free命令参数及用法详解(linux查看内存命令) 2019年05月31日 | 萬仟网科技 | 我要评论 free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段 ...

  10. Fresco用法详解

    版权声明:本文为延成原创文章,转载请标明出处 Fresco用法详解 经过在实际项目中多次的使用,本人在这做了一下简单总结,希望对初次使用和正在使用的你们有所帮助. 官方地址 官方github地址:ht ...

最新文章

  1. 使用Memcached实现Session共享
  2. SCVMM2012 SP1 之虚拟机模板的创建
  3. 转我们经理的一篇文章,业务流程实现的讨论,希望大家集思广议。
  4. linux安装python库报错pywin32_完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误...
  5. LeetCode动态规划 分割等和子集
  6. centos下apache安装
  7. 长沙试水数字人民币:线下支持数字人民币支付的商家已达3404个
  8. 加盟商最大的顾虑是什么?
  9. 旅行商回溯算法C语言,【算法作业】用回溯法求解旅行商问题
  10. 安卓暗黑模式软件_程序员欢呼!微软 GitHub 安卓版 App 发布预览:支持暗黑模式...
  11. Web多媒体:编解码器和容器
  12. JavaScript循环刷新页面
  13. 微信小程序获取后端数据
  14. 如何安装python380_python3.8下载及安装步骤详解
  15. 【HiFlow】新型零代码自动化助手
  16. 如何与低智商的人相处?
  17. word 插入背景 在背景上写字
  18. 江西省省赛中职网络安全-Windows操作系统渗透测试
  19. 什么是短信平台api接口?
  20. Toast的使用详解

热门文章

  1. MSN机器人-头像显示
  2. 如何查看文件md5值
  3. 数字证书是什么原理,有什么作用?
  4. Android下实现Google街景
  5. Visual Studio 2019 离线注册方法记录
  6. container-coding-codec
  7. 数据通信与计算机网复习题,数据通信与计算机网络 复习题总.doc
  8. mp4如何转换成wmv格式
  9. 大学计算机ppt学校题材实验报告,PPT实验报告模板2篇
  10. 电路布线问题的动态规划实现(java)