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>
相关文章推荐
- 5.轮播广告AndroidImageSlider的使用(包括自定义indicator,和sliderview的点击监听事件,架构分析)
- Android之事件监听的使用(OnClickListener)
- Android中Preference的使用以及监听事件分析
- Android之UI学习篇九:使用TabHost实现卡片选项菜单
- Android监听器实现(一)Broadcast方式监听系统事件
- Android中Preference的使用以及监听事件分析
- 使用jquery动态生成的标签,需要在代码当中绑定才可以实现事件的监听
- Android之使用Contentprovider对外共享数据和实现数据监听变化
- Android之UI学习篇十:使用TabHost实现微博客户端界面
- Android中Preference的使用以及监听事件分析
- Android 使用 OnTouchListener 接口监听双击或多击事件
- 使用“事件监听/链式事件处理方式”实现window.onload同时调用多个函数
- Android中Preference的使用以及监听事件分析
- 自定义android用户控件,使用回调函数实现自定义事件
- Android 小項目之---- 計算器 使用同一個事件监听完成 加减乘除 (附源码)
- Android中Preference的使用以及监听事件分析
- Android中Preference的使用以及监听事件分析
- Android中Preference的使用以及监听事件分析
- Android Preference 使用以及监听事件分析
- (转)Android中Preference的使用以及监听事件分析