两种方式使用tabhost/tabwidget布局

来源:互联网 发布:阿里云虚拟主机能干嘛 编辑:程序博客网 时间:2024/06/15 05:46

   之前在考虑设计一些客户端时,为了实现Activity下方的类似menu菜单的显示方式,一直傻傻的使用一个footer的布局文件,在这个footer中添加不同的按钮,然后在按钮上添加点击事件,根据点击事件切换到不同的Activity,这几天无聊的查看别人的代码时,才发现自己这种方式的老土,原来Android早就给我们提供了类似的显示方式了,即tabwidget(或者称之为tabhost布局)。

        我们的Android手机在很多的app中都使用了它,比如打电话、比如短信等等,今天我就整理下这两天使用的tabhsot布局,并使用两种方式呈现,效果图如下,点击时会有icon颜色变化。

使用TabActivity简单实现

        我们的Activity不再继承于Activity而改之继承于tabActivity,并在布局文件中布局上述点击按钮之后的5个显示文字的TextView,布局文件与之前的布局文件并无变化,布局文件代码如下:

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/t1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="tab1" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/t2"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="tab2" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/t3"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="tab3" />  
  24.   
  25.     <TextView  
  26.         android:id="@+id/t4"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="tab4" />  
  30.   
  31.     <TextView  
  32.         android:id="@+id/t5"  
  33.         android:layout_width="fill_parent"  
  34.         android:layout_height="wrap_content"  
  35.         android:text="tab5" />  
  36.   
  37. </LinearLayout>  

        在代码部分,我们需要做一些变动,代码不多,所以我先将代码提上来,然后再解释代码的重要地方。

Java代码
  1. public class TabViewTestActivity extends TabActivity{  
  2.     private Context mContext;  
  3.     private TabHost mTabHost;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         // TODO Auto-generated method stub  
  8.         super.onCreate(savedInstanceState);  
  9.         mContext = this;  
  10.         mTabHost = getTabHost();  
  11.           
  12.         LayoutInflater.from(mContext).inflate(R.layout.main, mTabHost.getTabContentView(), true);  
  13.           
  14.         mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("主题", getResources().getDrawable(R.drawable.theme_menu_theme)).setContent(R.id.t1));  
  15.         mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("壁纸", getResources().getDrawable(R.drawable.theme_menu_wallpaper)).setContent(R.id.t2));  
  16.         mTabHost.addTab(mTabHost.newTabSpec("C").setIndicator("背板", getResources().getDrawable(R.drawable.theme_menu_iconbg)).setContent(R.id.t3));  
  17.         mTabHost.addTab(mTabHost.newTabSpec("D").setIndicator("锁屏", getResources().getDrawable(R.drawable.theme_menu_screenlock)).setContent(R.id.t4));  
  18.         mTabHost.addTab(mTabHost.newTabSpec("E").setIndicator("特效", getResources().getDrawable(R.drawable.theme_menu_effect)).setContent(R.id.t5));  
  19.     }  
  20. }  

        代码中,我们定义一个mTabHost,我们看到了如果按照之前的方式,一个Activity引用布局文件中的布局的话,是使用setContent方法,而这会却是使用的LayoutInflater并在其的第二个参数中将viewGroup指定为getTabContentView。另外我们也看到了我们使用addTab方式给我们的tabWidget添加一个一个的tabSpec。

        最后运行效果如下:

名词解释:    既然我们使用tabhost来布局,我有必要对我们将要实现的代码做一个名词解释。

1、tabHost 这个我们可以简单的把它当作整个一张“Activity”,其中包括下面的托盘部分,以及点击托盘的任意按钮托盘上方显示的不同内容,这是最大的一个容器;

2、tabWidget 这个就是托盘部分,主要用来显示不同的栏目,其中包括tabSpec及tabContent;

3、tabSpec就是tabWidget的一个个栏目,你可以为它添加一张icon图片、icon图片下方的栏目名称,另外需要强调的是当用户点击任意 tabSpec时,tabWidget上方将显示内容,这个就是tabContent。

        源码下载:使用TabActivity简单实现TabHost显示

        

      我们在上述方式一中简单实现了TabHost的显示,但是我们在代码中主观控制TabHost的方法并不多,甚至在上述代码中,我们都没有见到托盘(TabWidget)的样式控制,所以我们也看到了该方式是将其默认的显示在最上方。但是当我们的需要是将之显示在下方,并且点击一个tab时会出现一些样式变化,比如图片更换等效果,这种方式就不灵活了,所以我们得自己提出自己来自己布局TabHost,使之样式多样化。 

        

自己布局TabHost,样式多样化

        在代码开始之前,我们需要对用到的名词进行解释下,如果你已经阅读了方式一,那么请略去!

名词解释:    既然我们使用tabhost来布局,我有必要对我们将要实现的代码做一个名词解释。

1、tabHost 这个我们可以简单的把它当作整个一张“Activity”,其中包括下面的托盘部分,以及点击托盘的任意按钮托盘上方显示的不同内容,这是最大的一个容器;

2、tabWidget 这个就是托盘部分,主要用来显示不同的栏目,其中包括tabSpec及tabContent;

3、tabSpec就是tabWidget的一个个栏目,你可以为它添加一张icon图片、icon图片下方的栏目名称,另外需要强调的是当用户点击任意 tabSpec时,tabWidget上方将显示内容,这个就是tabContent。

        既然是自己布局TabHost,我们在布局文件中就需自己一一来布局TabHost,TabWidget等等了,示例中的布局文件tab_main.xml代码如下:

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/tabhost"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:orientation="vertical" >  
  12.   
  13.         <TabWidget  
  14.             android:id="@android:id/tabs"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="60dip"  
  17.             android:layout_alignParentBottom="true"  
  18.             android:background="#424242" >  
  19.         </TabWidget>  
  20.   
  21.         <FrameLayout  
  22.             android:id="@android:id/tabcontent"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="fill_parent" >  
  25.   
  26.             <LinearLayout  
  27.                 android:id="@+id/theme"  
  28.                 android:layout_width="fill_parent"  
  29.                 android:layout_height="fill_parent"  
  30.                 android:orientation="vertical" >  
  31.   
  32.                 <TextView  
  33.                     android:id="@+id/theme_title"  
  34.                     android:layout_width="wrap_content"  
  35.                     android:layout_height="wrap_content"  
  36.                     android:text="主题" />  
  37.             </LinearLayout>  
  38.   
  39.             <LinearLayout  
  40.                 android:id="@+id/wallpaper"  
  41.                 android:layout_width="fill_parent"  
  42.                 android:layout_height="fill_parent"  
  43.                 android:orientation="vertical" >  
  44.   
  45.                 <TextView  
  46.                     android:id="@+id/wallpaper_title"  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:text="壁纸" />  
  50.             </LinearLayout>  
  51.   
  52.             <LinearLayout  
  53.                 android:id="@+id/iconbg"  
  54.                 android:layout_width="fill_parent"  
  55.                 android:layout_height="fill_parent"  
  56.                 android:orientation="vertical" >  
  57.   
  58.                 <TextView  
  59.                     android:id="@+id/iconbg_title"  
  60.                     android:layout_width="wrap_content"  
  61.                     android:layout_height="wrap_content"  
  62.                     android:text="背板" />  
  63.             </LinearLayout>  
  64.   
  65.             <LinearLayout  
  66.                 android:id="@+id/screenlock"  
  67.                 android:layout_width="fill_parent"  
  68.                 android:layout_height="fill_parent"  
  69.                 android:orientation="vertical" >  
  70.   
  71.                 <TextView  
  72.                     android:id="@+id/screenlock_title"  
  73.                     android:layout_width="wrap_content"  
  74.                     android:layout_height="wrap_content"  
  75.                     android:text="锁屏" />  
  76.             </LinearLayout>  
  77.   
  78.             <LinearLayout  
  79.                 android:id="@+id/effect"  
  80.                 android:layout_width="fill_parent"  
  81.                 android:layout_height="fill_parent"  
  82.                 android:orientation="vertical" >  
  83.   
  84.                 <TextView  
  85.                     android:id="@+id/effect_title"  
  86.                     android:layout_width="wrap_content"  
  87.                     android:layout_height="wrap_content"  
  88.                     android:text="特效" />  
  89.             </LinearLayout>  
  90.         </FrameLayout>  
  91.     </RelativeLayout>  
  92.   
  93. </TabHost>  

        从布局文件中,依次可以看到我们的布局文件需要实现的样式了,如果您看到了上面的名词解释再看这个布局文件就很好理解了,另外我们需要注意的两个地方我已经用红色背景画出来了:

        1、关于 TabWidget和FrameLayout的id,我们只能这样即使用系统自定义好的id;

        2、android:layout_alignParentBottom="true"表示我们的widget是在屏幕的底部显示。

        下面我们看java代码,在java代码中,我们无需继承TabActivity,依然和之前的一样继承Activity即可,代码如下:

Java代码
  1. public class TabDesignActivity extends Activity{  
  2.     private Context mContex = this;  
  3.     private TabHost mTabHost;  
  4.       
  5.     private String TAB1 = "主题";  
  6.     private String TAB2 = "壁纸";  
  7.     private String TAB3 = "背板";  
  8.     private String TAB4 = "锁屏";  
  9.     private String TAB5 = "特效";  
  10.       
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         // TODO Auto-generated method stub  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.tab_main);  
  16.           
  17.         mTabHost = (TabHost) findViewById(R.id.tabhost);  
  18.         mTabHost.setup();  
  19.           
  20.         mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getMenuItem(R.drawable.theme_ispressed, TAB1)).setContent(R.id.theme));  
  21.         mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getMenuItem(R.drawable.wallpaper_ispressed, TAB2)).setContent(R.id.wallpaper));  
  22.         mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(getMenuItem(R.drawable.iconbg_ispressed, TAB3)).setContent(R.id.iconbg));  
  23.         mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator(getMenuItem(R.drawable.screenlock_ispressed, TAB4)).setContent(R.id.screenlock));  
  24.         mTabHost.addTab(mTabHost.newTabSpec("tab5").setIndicator(getMenuItem(R.drawable.effect_ispressed, TAB5)).setContent(R.id.effect));  
  25.     }  
  26.       
  27.     public View getMenuItem(int imgID, String textID){  
  28.         LinearLayout ll = (LinearLayout) LayoutInflater.from(mContex).inflate(R.layout.tab_item, null);  
  29.         ImageView imgView = (ImageView)ll.findViewById(R.id.icon);  
  30.         imgView.setBackgroundResource(imgID);  
  31.         TextView textView = (TextView)ll.findViewById(R.id.name);  
  32.         textView.setText(textID);  
  33.         return ll;  
  34.     }     
  35. }  

       同样我们自己定义个mTabHost,但是这次我们获取mTabHost 是从布局文件中过来的,另外注意的是我们需要写上mTabHost.setup()这么一句代码,这个务必在addTab之前写进去,另外在给tabWidget添加tabSpec时,自己定义了一个getMenuItem方法来实现其图片和文字的显示的,这个方法的使用到的布局文件tab_item.xml如下:

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center_horizontal|center_vertical"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/ll"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/bg_ispressed"  
  13.         android:gravity="center_horizontal|center_vertical"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <ImageView  
  17.             android:id="@+id/icon"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content" />  
  20.   
  21.         <TextView  
  22.             android:id="@+id/name"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content" />  
  25.     </LinearLayout>  
  26.   
  27. </LinearLayout>  

        另外我使用了一个技巧来实现点击tab时换取图片的方式,即在图片文件中加上另一配置文件selector动态切换,关于此可参见【动态改变Tabwidget/tabhost的tab标签图片】:或者下载下面的源码。

        源码下载:自己布局TabHost,样式多样化显示


原创粉丝点击