Android TabHost 的美化与设计

用到的布局xml文件内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:id="@android:id/tabhost"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent">

  6.    <LinearLayout
  7.       android:orientation="vertical"
  8.       android:layout_width="fill_parent"
  9.       android:layout_height="fill_parent">
  10.       <FrameLayout
  11.          android:id="@android:id/tabcontent"
  12.          android:layout_width="fill_parent"
  13.          android:layout_height="wrap_content"
  14.          android:layout_weight="1">
  15.       </FrameLayout>
  16.       <TabWidget
  17.          android:id="@android:id/tabs"
  18.          android:layout_width="fill_parent"
  19.          android:layout_alignParentBottom="true"
  20.          android:layout_height="wrap_content"/>
  21.     </LinearLayout>
  22. </TabHost>
复制代码
//Activity 中抒写的代码:
  1. //记得先定义这几个变量
  2. private TabHost tabHost;
  3. private TabWidget tabWidget;
  4. Field mBottomLeftStrip;
  5. Field mBottomRightStrip;

  6. public TabHost makeTab(){
  7.         if(this.tabHost == null){
  8.                 tabHost = (TabHost)findViewById(android.R.id.tabhost);
  9.                 tabWidget = (TabWidget)findViewById(android.R.id.tabs);
  10.                 tabHost.setup();
  11.                 tabHost.bringToFront();

  12.                 TabSpec nearby_tab = tabHost.newTabSpec("nearby_tab");
  13.                 TabSpec history_tab = tabHost.newTabSpec("history_tab");
  14.                 TabSpec about_tab = tabHost.newTabSpec("about_tab");

  15.                 nearby_tab.setIndicator("first",getResources().getDrawable(R.drawable.first)).setContent(new Intent(this,first.class));
  16.                 history_tab.setIndicator("second",getResources().getDrawable(R.drawable.second)).setContent(new Intent(this,second.class));
  17.                 about_tab.setIndicator("third",getResources().getDrawable(R.drawable.third)).setContent(new Intent(this,third.class));

  18.                 tabHost.addTab(nearby_tab);
  19.                 tabHost.addTab(history_tab);
  20.                 tabHost.addTab(about_tab);

  21.                 if (Integer.valueOf(Build.VERSION.SDK) <= 7) {
  22.                         try {
  23.                                 mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip");
  24.                                 mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip");
  25.                                 if(!mBottomLeftStrip.isAccessible()) {
  26.                                         mBottomLeftStrip.setAccessible(true);
  27.                                 }
  28.                                 if(!mBottomRightStrip.isAccessible()){
  29.                                         mBottomRightStrip.setAccessible(true);
  30.                                 }
  31.                                 mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));
  32.                                 mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.linee));

  33.                         } catch (Exception e) {
  34.                                 e.printStackTrace();
  35.                         }
  36.                 } else {
  37.                         try {
  38.                                 mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mLeftStrip");
  39.                                 mBottomRightStrip = tabWidget.getClass().getDeclaredField("mRightStrip");
  40.                                 if (!mBottomLeftStrip.isAccessible()) {
  41.                                         mBottomLeftStrip.setAccessible(true);
  42.                                 }
  43.                                 if (!mBottomRightStrip.isAccessible()) {
  44.                                         mBottomRightStrip.setAccessible(true);
  45.                                 }
  46.                                 mBottomLeftStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
  47.                                 mBottomRightStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
  48.                         } catch (Exception e) {
  49.                                 // TODO Auto-generated catch block
  50.                                 e.printStackTrace();
  51.                         }
  52.                 }

  53.                 for (int i =0; i <tabWidget.getChildCount(); i++) {

  54.                         final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
  55.                         tv.setTextColor(Color.WHITE);
  56.                         View vvv = tabWidget.getChildAt(i);
  57.                         if(tabHost.getCurrentTab()==i){
  58.                                 vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.focus));
  59.                         }
  60.                         else {
  61.                                 vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.unfocus));
  62.                         }
  63.                 }

  64.                 tabHost.setOnTabChangedListener(new OnTabChangeListener(){

  65.                         @Override
  66.                         public void onTabChanged(String tabId) {
  67.                         // TODO Auto-generated method stub
  68.                         for (int i =0; i < tabWidget.getChildCount(); i++) {
  69.                                 View vvv = tabWidget.getChildAt(i);
  70.                                 if(tabHost.getCurrentTab()==i){
  71.                                         vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.focus));
  72.                                 }
  73.                                 else {
  74.                                         vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.unfocus));
  75.                                 }
  76.                         }
  77.                         }
  78.                 });
  79.         }
  80.         else{
  81.                 return tabHost;
  82.         }
  83.         return null;
  84. }
复制代码

效果图,求超越

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值