Android TabHost组件使用方法详解_Android

最近研究了一下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
, tabhost
组件
android四大组件详解、tabhost用法详解、fragmenttabhost 详解、tabhost详解、vue.js组件详解,以便于您获取更多的相关知识。

时间: 2024-08-30 09:05:42

Android TabHost组件使用方法详解_Android的相关文章

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" and

Android中HorizontalScrollView使用方法详解_Android

由于移动设备物理显示空间一般有限,不可能一次性的把所有要显示的内容都显示在屏幕上.所以各大平台一般会提供一些可滚动的视图来向用户展示数据.Android平台框架中为我们提供了诸如ListView.GirdView.ScrollView等滚动视图控件,这几个视图控件也是我们平常使用最多的.下面介绍一下HorizontalScrollView的使用和需要注意的点:  HorizontalScrollView是一个FrameLayout  ,这意味着你只能在它下面放置一个子控件,这个子控件可以包含很多

Android UI组件Spinner下拉列表详解_Android

Spinner下拉列表 该布局对应的关系图: 常用属性:android:entries(指定spinner要显示的字符串资源.必须是在strings资源文件中定义的字符串资源)android:spinnerMode(spinner的模式,枚举值有两个值dialog弹窗显示和dropdown下拉显示)android:dropDownWidth(下拉框的宽度,单位通常是dp)android:prompt(当spinnerMode的值是dialog时,弹出的对话框式的下列列表的提示.如果 spinne

Android编程闹钟设置方法详解_Android

本文实例讲述了Android编程闹钟设置方法.分享给大家供大家参考,具体如下: 闹钟在生活中最常见了,在Android中可以通过AlarmManager来实现闹钟,AlarmManager类专门用来设置在某个指定的时间去完成指定的时间.AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行.可以通过Context.getSystemService方法来获得该服务.AlarmManager中的方法不少,如下: 方法 说明 Cancel 取消

Android Volley框架使用方法详解_Android

本文主要从两个方面对Android Volley框架的使用方法进行讲解,具体内容如下 一.网络请求 1.get方式请求数据 // 1 创建一个请求队列 RequestQueue requestQueue = Volley.newRequestQueue(VolleyActivity.this); // 2 创建一个请求 String url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api"; StringRequest stri

Android结束进程的方法详解_Android

本文实例讲述了Android结束进程的方法.分享给大家供大家参考,具体如下: 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这个管理器结束掉其他的进程. 在Android平台下,结束进程的方法还是比较多的.首先指明,此处的"结束进程",包含了结束自身进程和结束其他进程两个方面.通过查阅SDK文档和网上的一些资料,自己找到一些结束进程的方法.在这里做一些归纳和总结,文章的部分信息有可能来自网上已有的文章和帖子,由于过了比较长时间,所以若发现本文与其他文章雷同,请谅解. 一.结

android 屏幕亮度调节方法详解_Android

屏幕亮度自动调节:主要是从Sensor分析之中分离出来分析LIGHT 光线感应器,因此就分析一下自动调节屏幕亮度(手机随着光线的强度自我调节,也就是在亮的光线下屏幕自动调亮一些,暗的时候就自动调暗一些,省得光线对眼睛有刺激).....     (本人从历经挫折才大概了解流程),现在就开始讲一下流程,如果有不对地方希望牛人指出来...     先从Sttings入手吧,在diaplay中有屏幕亮度调节,有一个进度调,上面有一个checkbox(自动调节屏幕亮度的),那么我们当然去找settings

基于linux与windows平台下 如何下载android sdk源代码的方法详解_Android

本文主要分2部份.第1部份介绍如何下载android sdk的源代码:第2部份介绍如何把android sdk的源代码加入到eclipse里 第1部份如何下载android sdk的源代码  1.环境. Linux 2.6 ,Python 2.4.3 或windows XP 2.工具.主要使用git工具,下载android sdk source. (1)Linux下git工具可以到 http://git-scm.com/ 下载,此工具是linux的核心代码管理工具. 安装过程如下: tar -x

深入Android MediaPlayer的使用方法详解_Android

1)如何获得MediaPlayer实例:可以使用直接new的方式:MediaPlayer mp = new MediaPlayer();也可以使用create的方式,如:MediaPlayer mp = MediaPlayer.create(this, R.raw.test);//这时就不用调用setDataSource了 2) 如何设置要播放的文件:MediaPlayer要播放的文件主要包括3个来源:a. 用户在应用中事先自带的resource资源例如:MediaPlayer.create(t