android实现标签页

本文介绍了如何在Android中实现标签页效果,通过XML布局文件和TabHost类来创建和设置各个标签,展示了一个包含三个标签页(Tab1、Tab2、Tab3)的应用示例,并提供了运行效果图。

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

Android自适应屏幕大小和layout布局 .

在我们的android手机中,经常会看到标签页的效果,那么,如下代码轻松实现该效果:
首先准备几张小图片放入drawable文件夹中。
1.main.xml布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
      <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@android:id/tabhost">
      <LinearLayout 
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <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/txt1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="this is a tab1."/>
      <TextView 
      android:id="@+id/txt2"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="this is a tab2."/>
      <TextView 
      android:id="@+id/txt3"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="this is a tab3."/>
      </FrameLayout>
      </LinearLayout>
      </TabHost>


2.TabWidgetActivity中代码:
package com.android.TabWidget;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class TabWidgetActivity extends TabActivity {
TabHost tabHost;
     
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          //取得tabhost对象
          tabHost=getTabHost();
         
          tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Tab1",getResources()
.getDrawable(R.drawable.img1)).setContent(R.id.txt1)
          );
          tabHost.addTab(tabHost.newTabSpec("tab_test2").setIndicator("Tab2",getResources()
.getDrawable(R.drawable.img2)).setContent(R.id.txt2)
          );
  tabHost.addTab(tabHost.newTabSpec("tab_test3").setIndicator("Tab3",getResources()
.getDrawable(R.drawable.img3)).setContent(R.id.txt3)
          );
          //设置背景颜色
          tabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
          //设置背景图片资源
          //tabHost.setBackgroundResource(R.drawable.bg0);
          //设置显示当前标签(设置第一个)
          tabHost.setCurrentTab(0);
          //调用线程
          tabHost.setOnTabChangedListener(new tabhosts());
      }

      class tabhosts implements OnTabChangeListener{
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
Dialog dialog=new AlertDialog.Builder(TabWidgetActivity.this)
.setTitle("提示")
.setMessage("当前选中:"+tabId+"标签")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();//创建按钮
dialog.show();
}
      }
}

3.运行效果图如下:
android实现标签页

android实现标签页

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值