tabHost的相关用法

来源:互联网 发布:java中排序函数 编辑:程序博客网 时间:2024/05/23 21:06

package com.cn.tab;

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.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class TabhostActivity extends TabActivity {
 // 声明TabHost对象
 TabHost mTabHost;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.two);

  // 取得TabHost对象
  mTabHost = getTabHost();
  setIndicator(R.drawable.img1, R.string.hq, R.id.first);
  setIndicator(R.drawable.img2, R.string.wt, R.id.first);
  setIndicator(R.drawable.img3, R.string.cx, R.id.first);
  setIndicator(R.drawable.img4, R.string.yz, R.id.first);
  setIndicator(R.drawable.img5, R.string.jl, R.id.first);
  setIndicator(R.drawable.img6, R.string.zx, R.id.first);

  // 设置TabHost的背景颜色
  mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
  // 设置TabHost的背景图片资源
  // mTabHost.setBackgroundResource(R.drawable.bg0);
  // 设置当前显示哪一个标签
  mTabHost.setCurrentTab(0);

  // 标签切换事件处理,setOnTabChangedListener
  mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
   // TODOAuto-generated method stub

   @Override
   public void onTabChanged(String tabId) {
    Dialog dialog = new AlertDialog.Builder(TabhostActivity.this)
      .setTitle("提示")
      .setMessage("当前选中:" + tabId + "标签")
      .setPositiveButton("确定",
        new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog,
           int whichButton) {
          dialog.cancel();
         }
        }).create();// 创建按钮

    dialog.show();
   }
  });

 }

 /*
  * private void setIndicator(int icon, int title, int view) { String str =
  * getResources().getString(title); TabHost.TabSpec localTabSpec =
  * mTabHost.newTabSpec(str) .setIndicator(str,
  * getResources().getDrawable(icon)) .setContent(view);
  * mTabHost.addTab(localTabSpec); }
  */
 private void setIndicator(int icon, int title, int view) {

  View localView = LayoutInflater.from(this.mTabHost.getContext())
    .inflate(R.layout.main_activity_tab, null);
  ((ImageView) localView.findViewById(R.id.main_activity_tab_image))
    .setBackgroundResource(icon);
  ((TextView) localView.findViewById(R.id.main_activity_tab_text))
    .setText(title);

  String str = getResources().getString(title);

  TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str)
    .setIndicator(localView).setContent(view);
  mTabHost.addTab(localTabSpec);

 }

}

 

<?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">
 
        
        <FrameLayout
         android:gravity="center"
         android:id="@android:id/tabcontent"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0.0" >
        
         <LinearLayout android:id="@+id/first"
                android:orientation="vertical"
                android:background="#ffffff"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
               >
                <TextView android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="first tab" />
            </LinearLayout>
            <LinearLayout android:id="@+id/second"
                android:orientation="vertical"
                android:background="#ffffff"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
                <TextView android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="second tab" />
            </LinearLayout>
        
        </FrameLayout>
      
        <TabWidget
         android:id="@android:id/tabs"
         android:background="@drawable/background"
         android:padding="3.0dip"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="0.0" />
      
    </LinearLayout>  
</TabHost> 

 

 

原创粉丝点击