
本文实例讲述了AndroID编程实现自定义Tab选项卡功能。分享给大家供大家参考,具体如下:
import androID.app.tabactivity;import androID.content.Intent;import androID.os.Bundle;import androID.Widget.*;import androID.Widget.TabHost.OnTabchangelistener;import androID.os.Build;import androID.vIEw.VIEw;import java.lang.reflect.FIEld;import androID.vIEw.LayoutInflater;public class testtabactivity extends tabactivity { /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int wIDth =45; int height =48; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); FIEld mBottomleftStrip; FIEld mBottomrightStrip; LayoutInflater.from(this).inflate(R.layout.tab_vIEws,tabs.getTabContentVIEw(),true); tabs.addTab(tabs.newTabSpec("first tab") .setIndicator("信息",getResources().getDrawable(R.drawable.m)) .setContent(new Intent(testtabactivity.this,OneActivty.class)) ); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("收藏",getResources().getDrawable(R.drawable.n)) .setContent(R.ID.content)); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("设置",getResources().getDrawable(R.drawable.s)) .setContent(R.ID.content)); for (int i =0; i < tabWidget.getChildCount(); i++) { /** * 设置高度、宽度,不过宽度由于设置为fill_parent,在此对它没效果 */ tabWidget.getChildAt(i).getLayoutParams().height = height; tabWidget.getChildAt(i).getLayoutParams().wIDth = wIDth; /** * 设置tab中标题文字的颜色,不然默认为黑色 */ final TextVIEw tv = (TextVIEw) tabWidget.getChildAt(i).findVIEwByID(androID.R.ID.Title); tv.setTextcolor(this.getResources().getcolorStateList(androID.R.color.white)); /** * 此方法是为了去掉系统默认的色白的底角 * * 在 TabWidget中mBottomleftStrip、mBottomrightStrip * 都是私有变量,但是我们可以通过反射来获取 * * 由于还不知道AndroID 2.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 (Exception e) { e.printstacktrace(); } } else { /** * 不做任何处理 */ } VIEw vvv = 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(new OnTabchangelistener(){ @OverrIDe public voID onTabChanged(String tabID) { // Todo auto-generated method stub for (int i =0; i < tabWidget.getChildCount(); i++) { VIEw vvv = 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程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现自定义Tab选项卡功能示例全部内容,希望文章能够帮你解决Android编程实现自定义Tab选项卡功能示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)