Android TabHost组件使用方法详解
作者:柴华松 发布时间:2023-11-14 20:01:13
标签:Android,TabHost,组件
最近研究了一下Contacts源码,仿照上面自己写了一个TabHostTest程序,现整理如下:
main.xml布局文件:
<?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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
</TabHost>
inner.xml文件:
<?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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</TabHost>
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中,在非主线程中更新UI控件是不安全的,app在运行时会直接Crash,所以当我们需要在非主线程中更新UI控件,那么就需要用
- Service翻译成中文是服务,熟悉Windows 系统的同学一定很熟悉了。A
- 简介相机模块库,自定义相机,通过简单的调用即可实现拍照、图片裁剪、录像及录像抓拍功能;实现图片压缩,减少图片体积;自定义相机可避免使用系统相
- 场景:假设每次我们去超市购物,我们都会推一个购物车,有水果、蔬菜、肉类三种商品,提供给我们选择,那么这时候,如果使用装饰器模式,应该如何实现
- 本文所述为基于C#实现的三层架构。对于三层的概念查相信大家并不陌生,这里举一个关于三层的简单实例,真正看一下它是如何具体实现的.我们先来一起
- 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后除以10的余数代替该数字,再将第一
- 本文实例讲述了C#获取字符串后几位数的方法。分享给大家供大家参考。具体实现方法如下:#region 获取后几位数 public string
- 这个问题来来回回困扰了我很久,一直没能妥善解决。场景1:华为手机遮挡了屏幕底部。场景2:进入应用时,虚拟键自动缩回,留下空白区域。需求:需要
- 1.概述:C语言中的单向链表(单链表)是链表的一种,其特点是链表的链接方向是单向的,对链表的访问要通过顺序读取从头部开始。链表中最简单的一种
- 本博文将为您提供自Java 7以来增加的很棒的新功能的示例。我将展示每个Java版本的至少一项重大改进,一直到2020年秋季发布的Java
- using System; using System.Collections.Generic; using System.Linq; usi
- 一、引入pom<?xml version="1.0" encoding="UTF-8"?>
- 1. 使用蓝牙的响应权限<uses-permission android:name="android.permission.
- 在学习安卓的最初过程中我们学的都是最基本的一个活动,只有一个活动的应用也太简单了吧,没错我们的最求应该更高点,不管你创建多少个活动,接下里我
- 1. 抽象类关键字:abstract类:用来描述一类具体的事物抽象类:抽象的、模糊的、不具体的类在Java的普通类中是不允许多继承的,原因是
- 一:Android 中Home键监听和Back键监听的区别:(1).在Android中,当按下Home键的时候,默认情况下Stop前台的Ac
- try { using (TransactionScope tr = new Transact
- 一、pom.xml引入相关模块web、jpa、thymeleaf、oracle:<dependency> &nbs
- 本文介绍restTemplate基础用法。Java中get和post的用法请参考:一文带你搞懂Java中Get和Post的使用1 提供get
- @Value取值为NULL的问题在spring mvc架构中,如果希望在程序中直接使用properties中定义的配置值,通常使用一下方式来