Android编程实现自定义Tab选项卡功能示例
本文实例讲述了Android编程实现自定义Tab选项卡功能。分享给大家供大家参考,具体如下:
importandroid.app.TabActivity; importandroid.content.Intent; importandroid.os.Bundle; importandroid.widget.*; importandroid.widget.TabHost.OnTabChangeListener; importandroid.os.Build; importandroid.view.View; importjava.lang.reflect.Field; importandroid.view.LayoutInflater; publicclasstestTabActivityextendsTabActivity{ /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); intwidth=45; intheight=48; finalTabHosttabs=getTabHost(); finalTabWidgettabWidget=tabs.getTabWidget(); FieldmBottomLeftStrip; FieldmBottomRightStrip; LayoutInflater.from(this).inflate(R.layout.tab_views,tabs.getTabContentView(),true); tabs.addTab(tabs.newTabSpec("firsttab") .setIndicator("信息",getResources().getDrawable(R.drawable.m)) .setContent(newIntent(testTabActivity.this,OneActivty.class)) ); tabs.addTab(tabs.newTabSpec("secondtab") .setIndicator("收藏",getResources().getDrawable(R.drawable.n)) .setContent(R.id.content)); tabs.addTab(tabs.newTabSpec("secondtab") .setIndicator("设置",getResources().getDrawable(R.drawable.s)) .setContent(R.id.content)); for(inti=0;i<tabWidget.getChildCount();i++){ /** *设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果 */ tabWidget.getChildAt(i).getLayoutParams().height=height; tabWidget.getChildAt(i).getLayoutParams().width=width; /** *设置tab中标题文字的颜色,不然默认为黑色 */ finalTextViewtv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList(android.R.color.white)); /** *此方法是为了去掉系统默认的色白的底角 * *在TabWidget中mBottomLeftStrip、mBottomRightStrip *都是私有变量,但是我们可以通过反射来获取 * *由于还不知道Android2.2的接口是怎么样的,现在先加个判断好一些 */ if(Float.valueOf(Build.VERSION.RELEASE)<=2.1){ try{ mBottomLeftStrip=tabWidget.getClass().getDeclaredField("mBottomLeftStrip"); mBottomRightStrip=tabWidget.getClass().getDeclaredField("mBottomRightStrip"); if(!mBottomLeftStrip.isAccessible()){ mBottomLeftStrip.setAccessible(true); } if(!mBottomRightStrip.isAccessible()){ mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget,getResources().getDrawable(R.drawable.no)); mBottomRightStrip.set(tabWidget,getResources().getDrawable(R.drawable.no)); }catch(Exceptione){ e.printStackTrace(); } }else{ /** *不做任何处理 */ } Viewvvv=tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else{ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } /** *当点击tab选项卡的时候,更改当前的背景 */ tabs.setOnTabChangedListener(newOnTabChangeListener(){ @Override publicvoidonTabChanged(StringtabId){ //TODOAuto-generatedmethodstub for(inti=0;i<tabWidget.getChildCount();i++){ Viewvvv=tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else{ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } }}); } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程开发之SD卡操作方法汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。