1、activity_tabwidget.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="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TabWidget>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/tb_tv1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="This is the first tab" />

<TextView
android:id="@+id/tb_tv2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="This is another tab" />

<TextView
android:id="@+id/tb_tv3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="This is the third tab" />
</FrameLayout>
</LinearLayout>

</TabHost>

2、TabWidgetActivity.java

public class TabWidgetActivity extends TabActivity {
// 声明TabHost对象
TabHost tabHost;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabwidget);
try {

// 取得TabHost对象
tabHost = getTabHost();
/* 为TabHost添加标签 */
// 新建一个newTabSpec(newTabSpec)
// 设置其标签和图标(setIndicator)
// 设置内容(setContent)
tabHost.addTab(tabHost
.newTabSpec("tab_test1")
.setIndicator("TAB1",
getResources().getDrawable(R.drawable.button1))
.setContent(R.id.tb_tv1));
tabHost.addTab(tabHost
.newTabSpec("tab_test2")
.setIndicator("TAB2",
getResources().getDrawable(R.drawable.button2))
.setContent(R.id.tb_tv2));
tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("TAB3",
getResources().getDrawable(R.drawable.button3)).setContent(R.id.tb_tv3));

// 设置TabHost的背景颜色
tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
// 设置TabHost的背景图片资源
tabHost.setBackgroundResource(R.drawable.ic_launcher);
// 设置当前显示那一个标签
tabHost.setCurrentTab(0);
} catch (Exception e) {
e.printStackTrace();
}
// 标签切换事件处理
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {
Dialog dialog = new AlertDialog.Builder(TabWidgetActivity.this)
.setTitle("提示")
.setMessage("当前选中" + tabId + "标签")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.cancel();
}
}).create();
dialog.show();
}
});

}
}

更多相关文章

  1. Android webview与js 交换JSON对象数据
  2. android 上 webkit js 扩展之全局本地对象实现步骤
  3. android Parcelable接口序列化对象
  4. 【Android 初学】10、Intent对象的使用
  5. Android EditText 共用TextWatcher,在TextWatcher中确定对应的被
  6. Android 时间对象操作工具类
  7. Android中的Message类以及Java对象池的实现
  8. AndroidMainifest标签使用说明3——<activity-alias>

随机推荐

  1. 从函数返回2个值[duplicate]
  2. 如何使用用户名作为子域名创建子域?
  3. PHP生成圆心图片-常用作头像圆图等场景
  4. hust校赛d题 PHP is the best language i
  5. 存储用户所需语言的最佳方式
  6. PHP中使用cURL实现Get和Post请求的方法
  7. 一些PHP相关的数据比较
  8. Windows7搭建Apache本地服务器+PHP环境
  9. 如何为$ wpdb-> insert创建一个包含colum
  10. 为什么cron工作不是从浏览器工作,但从终端