TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

  • 如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
  • TabWidget必须设置android:id为@android:id/tabs
  • FrameLayout需要设置android:id为@android:id/tabcontent

但是,实践告诉我们,建立这个布局文件不一定要同时存在,在android的说明demo中,是使用的。布局如下:

<?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="fill_parent"  android:layout_height="fill_parent">  <LinearLayout    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="5dp">    <TabWidget      android:id="@android:id/tabs"      android:layout_width="fill_parent"      android:layout_height="wrap_content" />    <FrameLayout      android:id="@android:id/tabcontent"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:padding="5dp" />  </LinearLayout></TabHost>

这样也是可以的。但是还可以这样动态做。(没有TabWidget和FrameLayout)

布局文件如下:

<?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="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp"
android:id="@+id/tab1"
>
<!--

<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dp" />

-->


<Button android:id="@+id/redirect" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="切换到第三个标签" />
<ImageView android:id="@+id/imageview" android:layout_width="fill_parent"
android:layout_height="300dp" android:background="#000000"
android:src="@drawable/background" />
</LinearLayout>


</TabHost>

那么和demo中不同之后,我们Activity中应该这样。

不用:// setContentView(R.layout.main);

而是这样装载:

//装载main.xml布局文件
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
// intent=new Intent();
//intent.setClass(this,ArtistsActivity.class);
//为第一个tab初始化TabSpec并加入TabHost
spec=tabHost.newTabSpec("tab1");
spec.setIndicator("第一个标签", res.getDrawable(R.drawable.ic_tab_artists_grey));
spec.setContent(R.id.tab1);
tabHost.addTab(spec);

这样之后,第一个标签显示的就是我们的main.xml布局。main.xml布局上面有给出。明明没有TabWidget和FrameLayout这两个。为什么还可以运行TabHost呢。

原因就是我们动态装载了。而这种方法往往比较实用。

LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);

比如说:如果在第一个Tab里面有一个按钮,点击之后跳到第二个或者第三个tab。这样的话,只能使用

getTabHost().setCurrentTabByTag("课程列表");

Activity中的代码(必须继承TabActivity)

package com.xiehande.tabwidget.activity;

import org.apache.http.impl.client.TunnelRefusedException;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {
Button redirect;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);

//获取Resources
Resources res=getResources();
//获取TabHost
TabHost tabHost=getTabHost();
//获取TabHost.TabSpec
TabHost.TabSpec spec;
//获取Intent
Intent intent ;
//装载main.xml布局文件
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);
// intent=new Intent();
//intent.setClass(this,ArtistsActivity.class);
//为第一个tab初始化TabSpec并加入TabHost
spec=tabHost.newTabSpec("tab1");
spec.setIndicator("第一个标签", res.getDrawable(R.drawable.ic_tab_artists_grey));
spec.setContent(R.id.tab1);
tabHost.addTab(spec);

intent=new Intent().setClass(this, AlbumsActivity.class);
//为第二个tab初始化TabSpec并加入TabHost
spec=tabHost.newTabSpec("albums");
spec.setIndicator("相册", res.getDrawable(R.drawable.ic_tab_artists_grey));
spec.setContent(intent);
tabHost.addTab(spec);

//为第三个tab初始化TabSpec并加入TabHost
intent=new Intent().setClass(this, SongsActivity.class);
spec=tabHost.newTabSpec("课程列表").setIndicator("课程列表", res.getDrawable(R.drawable.ic_tab_artists_grey)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);

redirect=(Button) findViewById(R.id.redirect);
redirect.setOnClickListener(listenter);

}
private OnClickListener listenter=new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getTabHost().setCurrentTabByTag("课程列表");
}
};

}

更多相关文章

  1. Android(安卓)GPS 获取
  2. android 获取路径目录方法以及判断目录是否存在,创建目录
  3. Android(安卓)Tab 控件详解及实例
  4. 如何在Android平台上创建自定义的Cordova插件并使用SAP UI5消费
  5. Android之SharedPreferences管理类AppPreferences
  6. android 获取系统电量
  7. Android(安卓)获取控件的宽和高
  8. android获取安装的应用程序
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. Android7.0中文文档(API)-- RemoteViews
  2. android单元测试
  3. Android(安卓)Support Library 23.2
  4. Android(安卓)面试题及答案(英文)
  5. android的ExpandableListView
  6. android FactoryReset
  7. i.MX android 2.3 R10.3.1 环境( at ubun
  8. SlidingLayout
  9. android > Android(安卓)音频均衡器,可通
  10. Android(安卓)封装的数据库管理操作类