android tabhost间距,TabHost的使用及布局文件

这个示例展示了如何在Android中使用TabActivity创建带有图标和文字指示器的标签页,并实现标签切换时的监听。当用户切换标签时,会弹出一个对话框显示当前选中的标签页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.zxl;

import android.app.AlertDialog;

import android.app.TabActivity;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.graphics.Color;

import android.os.Bundle;

import android.widget.TabHost;

import android.widget.TabHost.OnTabChangeListener;

public class Activity01 extends TabActivity {

TabHost tabHost;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tabHost = getTabHost();

tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("TAB

1",

getResources().getDrawable(R.drawable.face107)).setContent(

R.id.textView1));

tabHost.addTab(tabHost.newTabSpec("tab_test2").setIndicator("TAB

2",

getResources().getDrawable(R.drawable.face108)).setContent(

R.id.textView2));

tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("TAB

3",

getResources().getDrawable(R.drawable.face109)).setContent(

R.id.textView3));

tabHost.setBackgroundColor(Color.RED);

tabHost.setBackgroundResource(R.drawable.splash_background);

tabHost.setCurrentTab(0);

tabHost.setOnTabChangedListener(new

OnTabChangeListener() {

AlertDialog.Builder

alertDialog;

@Override

public void

onTabChanged(String tabId) {

//

TODO Auto-rgenerated method stub

alertDialog=new

AlertDialog.Builder(Activity01.this);

alertDialog.setTitle("提示");

alertDialog.setMessage("

当前选中:"+tabId+"标签");

alertDialog.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public

void onClick(DialogInterface dialog, int which) {

//

TODO Auto-generated method stub

}

});

alertDialog.create();

alertDialog.show();

}

});

}

layout中布局文件 main.xml

encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android">

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@android:id/tabs"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@android:id/tabcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/textView1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="这是一张切换卡" />

android:id="@+id/textView2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="这是另一张切换卡" />

android:id="@+id/textView3"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="这是第三张切换卡" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值