底部tabhost研究一(TabHost 不是最外面的布局)

 

  各位大侠请自行把代码放到项目中去。没有给的activity自己手写几个。然后替换就可以了     运行不出来的请联系我。

底部tabhost研究一(TabHost 不是最外面的布局)

 

[java]  view plain copy print ?
  1. import android.app.ActivityGroup;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.widget.TabHost;  
  5. import android.widget.TabHost.TabSpec;  
  6.   
  7. public class MainActivity extends ActivityGroup {  
  8.     public  TabHost tab_host;  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);  
  14.         tab_host.setup(this.getLocalActivityManager());  
  15.         tab_host.setPadding(tab_host.getPaddingLeft(),  
  16.                 tab_host.getPaddingTop(), tab_host.getPaddingRight(),  
  17.                 tab_host.getPaddingBottom()-10);  
  18.         TabSpec ts1 = tab_host.newTabSpec("TAB_RECOMMEND");  
  19.         ts1.setIndicator("s", getResources().getDrawable(R.drawable.icon));  
  20.         ts1.setContent(new Intent(this, TestActivity.class));  
  21.         tab_host.addTab(ts1);  
  22.   
  23.         TabSpec ts2 = tab_host.newTabSpec("TAB_NEWLY");  
  24.         ts2.setIndicator("s", getResources().getDrawable(R.drawable.icon));  
  25.         ts2.setContent(new Intent(this, TestActivity.class));  
  26.         tab_host.addTab(ts2);  
  27.   
  28.         TabSpec ts3 = tab_host.newTabSpec("TAB_PHB");  
  29.         ts3.setIndicator("s", getResources().getDrawable(R.drawable.icon));  
  30.         ts3.setContent(new Intent(this, TestActivity.class));  
  31.         tab_host.addTab(ts3);  
  32.   
  33.         tab_host.setCurrentTab(0);  
  34.     }  
  35.     
  36. }  

 

[java]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TabHost android:id="@+id/edit_item_tab_host"  
  6.         android:layout_width="fill_parent" android:layout_height="fill_parent">  
  7.         <LinearLayout android:orientation="vertical"  
  8.             android:layout_width="fill_parent" android:layout_height="fill_parent"  
  9.             android:padding="5dp">  
  10.             <FrameLayout android:id="@android:id/tabcontent"  
  11.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  12.                 android:padding="5dp" android:layout_weight="1" />  
  13.             <TabWidget android:id="@android:id/tabs"  
  14.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  15.                 android:layout_weight="0" />  
  16.         </LinearLayout>  
  17.     </TabHost>  
  18. </LinearLayout>  



 

如果把图片做为按钮      去掉外面黑色的背景      加上

 

[java]  view plain copy print ?
  1. /* 对Tab标签的定制 */  
  2.      mTabWidget = mTabHost.getTabWidget();  
  3.      for (int i = 0; i < mTabWidget.getChildCount(); i++)  
  4.      {  
  5.          /* 得到每个标签的视图 */  
  6.          View view = mTabWidget.getChildAt(i);  
  7.          /* 设置每个标签的背景 */  
  8.          if (mTabHost.getCurrentTab() == i)  
  9.          {  
  10.             if(i==0){  
  11.                 view.setBackgroundDrawable(getResources().getDrawable(  
  12.                          R.drawable.guide_menu1));  
  13.             }else if(i==1){  
  14.                 view.setBackgroundDrawable(getResources().getDrawable(  
  15.                          R.drawable.guide_menu3));  
  16.             }else if(i==2){  
  17.                 view.setBackgroundDrawable(getResources().getDrawable(  
  18.                          R.drawable.guide_menu5));  
  19.             }else if(i==3){  
  20.                 view.setBackgroundDrawable(getResources().getDrawable(  
  21.                          R.drawable.guide_menu7));  
  22.             }  
  23.              //view.setBackgroundDrawable(getResources().getDrawable(  
  24.              //        R.drawable.home_btn_bg_d));  
  25.          }  
  26.          else  
  27.          {  
  28.             if(i==0){  
  29.                 view.setBackgroundDrawable(getResources().getDrawable(  
  30.                          R.drawable.guide_menu0));  
  31.             }else if(i==1){  
  32.                 view.setBackgroundDrawable(getResources().getDrawable(  
  33.                          R.drawable.guide_menu2));  
  34.             }else if(i==2){  
  35.                 view.setBackgroundDrawable(getResources().getDrawable(  
  36.                          R.drawable.guide_menu4));  
  37.             }else if(i==3){  
  38.                 view.setBackgroundDrawable(getResources().getDrawable(  
  39.                          R.drawable.guide_menu6));  
  40.             }  
  41.              //view.setBackgroundDrawable(getResources().getDrawable(  
  42.              //        R.drawable.home_btn_bg_n));  
  43.          }  
  44.          /* 设置Tab间分割竖线的颜色 */  
  45.          // tabWidget.setBackgroundColor(Color.WHITE);  
  46.          /* 设置Tab间分割竖线的背景图片 */  
  47.          // tabWidget.setBackgroundResource(R.drawable.icon);  
  48.          /* 设置tab的高度 */  
  49.          mTabWidget.getChildAt(i).getLayoutParams().height = 50;  
  50.          TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(  
  51.                  android.R.id.title);  
  52.          tv.setTextSize(18);  
  53.          /* 设置tab内字体的颜色 */  
  54.          //tv.setTextColor(Color.rgb(49, 116, 171));  
  55.      }  
  56.      /* 当点击Tab选项卡的时候,更改当前Tab标签的背景 */  
  57.      mTabHost.setOnTabChangedListener(new OnTabChangeListener()  
  58.      {  
  59.          @Override  
  60.          public void onTabChanged(String tabId)  
  61.          {  
  62.              for (int i = 0; i < mTabWidget.getChildCount(); i++)  
  63.              {  
  64.                  View view = mTabWidget.getChildAt(i);  
  65.                  if (mTabHost.getCurrentTab() == i)  
  66.                  {  
  67.                     if(i==0){  
  68.                         view.setBackgroundDrawable(getResources().getDrawable(  
  69.                                  R.drawable.guide_menu1));  
  70.                     }else if(i==1){  
  71.                         view.setBackgroundDrawable(getResources().getDrawable(  
  72.                                  R.drawable.guide_menu3));  
  73.                     }else if(i==2){  
  74.                         view.setBackgroundDrawable(getResources().getDrawable(  
  75.                                  R.drawable.guide_menu5));  
  76.                     }else if(i==3){  
  77.                         view.setBackgroundDrawable(getResources().getDrawable(  
  78.                                  R.drawable.guide_menu7));  
  79.                     }  
  80.                      //view.setBackgroundDrawable(getResources().getDrawable(  
  81.                      //        R.drawable.home_btn_bg_d));  
  82.                  }  
  83.                  else  
  84.                  {  
  85.                     if(i==0){  
  86.                         view.setBackgroundDrawable(getResources().getDrawable(  
  87.                                  R.drawable.guide_menu0));  
  88.                     }else if(i==1){  
  89.                         view.setBackgroundDrawable(getResources().getDrawable(  
  90.                                  R.drawable.guide_menu2));  
  91.                     }else if(i==2){  
  92.                         view.setBackgroundDrawable(getResources().getDrawable(  
  93.                                  R.drawable.guide_menu4));  
  94.                     }else if(i==3){  
  95.                         view.setBackgroundDrawable(getResources().getDrawable(  
  96.                                  R.drawable.guide_menu6));  
  97.                     }  
  98.                      //view.setBackgroundDrawable(getResources().getDrawable(  
  99.                      //        R.drawable.home_btn_bg_n));  
  100.                  }  
  101.              }  
  102.          }  
  103.      });  

你可能感兴趣的:(tabhost)