android使用TabHost实现tabbar,包括监听事件

/**   
* @Title: GatewayActivity.java 
* @Package com.yisa.qiqilogin.activity 
* @Description: TODO(用一句话描述该文件做什么) 
* @author hq  
* @date 2015年1月8日 下午1:53:44 
* @version V1.0   
*/
package com.yisa.qiqilogin.activity;

import com.yisa.qiqilogin.R;
import com.yisa.qiqilogin.service.NotificationService;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

/** 
 * @ClassName: GatewayActivity 
 * @Description: 门户页
 * @author hq 
 * @date 2015年1月8日 下午1:53:44 
 *  
 */
public class GatewayActivity extends TabActivity {
	private TabHost tabHost;
	private TabWidget mTabWidget;
	
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		setContentView(R.layout.layout_gateway);
		String userName = intent.getStringExtra("username");
 		//启动通知的service
 		Intent notiSevrIntent = new Intent(this, NotificationService.class);
 		startService(notiSevrIntent);
 		
 		
        //获取TabHost对象
// 		Intent intent2 = new Intent(GatewayActivity.this,  NoticeCenter.class);
        tabHost = getTabHost();  
        mTabWidget = tabHost.getTabWidget(); 
        
//        tabHost.setup();
        //新建一个newTabSpec,设置标签和图标(setIndicator),设置内容(setContent)
        tabHost.addTab(tabHost.newTabSpec("homepage").setIndicator("",getResources().getDrawable(R.drawable.homenormal)).setContent(R.id.tabFirst));
        tabHost.addTab(tabHost.newTabSpec("userinfo").setIndicator("",getResources().getDrawable(R.drawable.categorynormal)).setContent(R.id.tabSecond));
        tabHost.addTab(tabHost.newTabSpec("noticecenter").setIndicator("",getResources().getDrawable(R.drawable.personnormal)).setContent(R.id.lastTab));
        //设置TabHost的背景颜色
        tabHost.setBackgroundColor(Color.argb(150,22,70,150));
        //设置TabHost的背景图片资源
//      tabHost.setBackgroundResource(R.drawable.bg);

        View v = mTabWidget.getChildTabViewAt(2);
        v.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(GatewayActivity.this, NoticeCenter.class);
        		startActivity(intent);
			}
        	
        });
        //设置当前现实哪一个标签
        tabHost.setCurrentTab(0);   //0为标签ID
        //标签切换处理,用setOnTabChangedListener  
        tabHost.setOnTabChangedListener(new OnTabChangeListener(){
        	@Override
            public void onTabChanged(String tabId){
            	Log.d("GatewayActivity--tabId--=", tabId);
//                Toast.makeText(TabTestActivity.this, "This is a Test!", Toast.LENGTH_LONG).show();
//            	if("noticecenter".equals(tabId)){
//            		Log.d("GatewayActivity--success--=", tabId);
//            		Intent intent = new Intent(GatewayActivity.this, NoticeCenter.class);
//            		startActivity(intent);
//            	}
            }
        });
	}
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">
    <RelativeLayout
        android:background="@drawable/g"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
	    <TabWidget android:id="@android:id/tabs"
	        android:layout_width="fill_parent"
	        android:layout_height="40dp"
	        android:layout_alignParentBottom="true">
	    </TabWidget>
	    <FrameLayout android:id="@android:id/tabcontent"
	        android:layout_width="fill_parent"
	        android:layout_height="fill_parent">
	        <RelativeLayout android:id="@+id/tabFirst"
	            android:layout_width="fill_parent"
	            android:layout_height="fill_parent"
	            android:orientation="vertical">
	        <DigitalClock
	                android:layout_centerInParent="true"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content">
	            </DigitalClock>
	        </RelativeLayout>
	        <RelativeLayout android:id="@+id/tabSecond"
	            android:layout_width="fill_parent"
	            android:layout_height="fill_parent"
	            android:orientation="vertical">
	            <DigitalClock
	                android:layout_centerInParent="true"
	                android:layout_width="wrap_content"
	                android:layout_height="wrap_content">
	            </DigitalClock>
	        </RelativeLayout>
	        <RelativeLayout android:id="@+id/lastTab"
	            android:layout_width="fill_parent"
	            android:layout_height="fill_parent"
	            android:orientation="vertical">
	        </RelativeLayout>
   		</FrameLayout>
    </RelativeLayout>
</TabHost>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值