android实现标签页

来源:互联网 发布:电脑数据丢失如何恢复 编辑:程序博客网 时间:2024/05/22 05:07
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实现标签页

原创粉丝点击