您的位置:首页 > 移动开发 > Android开发

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

2015-01-09 17:38 357 查看
/**
* @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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: