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)));
}
}
效果图如下:
0
投稿
猜你喜欢
- 1. 可变参数在JDK1.5之后,如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化.格式:修饰符 返回值类型 方
- 本文实例讲述了C#泛型委托的用法。分享给大家供大家参考。具体分析如下:冒泡排序大家都知道,例如一个整形数组,可以用冒泡排序来使它按从小到大的
- 1.分页类package org.zh.basic;/** * 页面类 * * @author keven&
- 不用单点登录,模拟远程项目的登录页面表单,在访问这个页面的时候自动提交表单到此项目的登录action,就可以实现登录到其他系统。ssh框架项
- 题目我们可以用2×1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2×1的小矩形无重叠地覆盖一个2×n的大矩形,总共有多少种方法?程序核心
- 先引用using System.Runtime.InteropServices; 的命名空间, 然后在合适的位置加上如下代码就OK。。注意:
- 1. 配置 * 具体步骤:编写一自定义 * 类实现接口 HandlerInterceptorHandlerInterceptor 接口: 可
- 这一定是困扰刚开始使用idea工具同学的一个大问题。三种情况会导致这种问题出现。1、你不小心按了键盘上的insert按键解决:再按一次吧2、
- 生成文字图片:/// <summary> /// 生成文字图片 /// &l
- 本文实例讲述了Java编程实现非对称加密的方法。分享给大家供大家参考,具体如下:对称加密算法在加密和解密时使用的是同一个秘钥;而非对称加密算
- 最近有个项目的几张表,数量级在千万以上,技术栈是SpringBoot+Mybatis-plus+MySQL。如果使用单表,在进行查询操作,非
- 本文实例为大家分享了javaOpenCV-4.0.0 实时人脸识别,供大家参考,具体内容如下package com.xu.opencv;im
- Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务
- 问题:什么是自旋锁?说一下 synchronized 底层实现原理?多线程中 synchronized 锁升级的原理是什么?1. 轻量级锁的
- 前言日常开发中,特别是音视频开发,需要在界面上渲染视频,比如制作一个播放器、或者视频编辑工具、以及视频会议客户端。通常拿到的是像素格式数据,
- 一、加密方案介绍对接口的加密解密操作主要有下面两种方式:自定义消息转换器优势:仅需实现接口,配置简单。劣势:仅能对同一类型的MediaTyp
- 主要完成任务:1.read read 并行化2.read write 不允许3.write write 不允许public class Re
- 一、Java异常架构与异常关键字Java异常简介Java 异常是 Java 提供的一种识别及响应错误的一致性机制。Java 异常机制可以使程
- 前言本文主要给大家介绍了如何更改Dialog的标题与按钮颜色的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。an
- 本文实例讲述了c#中datagridview处理非绑定列的方法。分享给大家供大家参考。具体实现方法如下:using System;using