阅读127 返回首页    go 阿里云 go 技术社区[云栖]


创新源于模仿之一:TabActivity的美化

 

今天开始一个新专题:创新源于模仿。

第一篇从TabActivity着手,一直以为Android中的TabActivity只能放在上面,只能如此丑陋,直到有一天看到“米聊”。



咋一看,软件下面的那个菜单栏,觉得像是用LinearLayout+Button来实现的,但事实上,它却是一个Tab!
怎么看出来的?我就不多说了,你懂的。

下面我们来抽丝剥茧,一步步分析它的实现过程。

1.TabActivity的布局


  1. <TabHost xmlns:androhttps://schemas.android.com/apk/res/android">https://schemas.android.com/apk/res/android"
     android:
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
      >
     <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      
             
            <FrameLayout
             android:gravity="center"
             android:
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="1.0" >
             
             <LinearLayout android:
                    android:orientation="vertical"
                    android:background="#ffffff"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                   >
                    <TextView android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="first tab" />
                </LinearLayout>
                <LinearLayout android:
                    android:orientation="vertical"
                    android:background="#ffffff"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    >
                    <TextView android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:text="second tab" />
                </LinearLayout>
             
            </FrameLayout>
           
            <TabWidget
             android:
             android:background="@drawable/tab_bottom_bg"
             android:padding="3.0dip"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_weight="0.0" />
           
        </LinearLayout>   
    </TabHost>  

 

 

我们看到,要自定义一个位于屏幕下方的TAB标签,首先我们不能使用缺省的TabActivity实现了,啥事都要自己亲力亲为。
很好理解,把tabs放在tabcontent下面就可以了。其它不多说了。


2.MainActivity的实现代码


先看看缺省的实现代码,不复杂,省略一些无关的东西:

 

private void setIndicator(int icon, int title, int view) { 
        String str = getResources().getString(title); 
         
        TabHost.TabSpec localTabSpec=tabhost.newTabSpec(str).setIndicator(str,getResources().getDrawable(icon)).setContent(view); 
        tabhost.addTab(localTabSpec); 
    } 
private void setIndicator(int icon, int title, int view) {
     String str = getResources().getString(title);
     
     TabHost.TabSpec localTabSpec=tabhost.newTabSpec(str).setIndicator(str,getResources().getDrawable(icon)).setContent(view);
     tabhost.addTab(localTabSpec);
    }

 

可以测试一下,效果出来了吧,虽然有点丑,但它真的在屏幕下方了。

 

3.美化TAB标签

 

现在我们来定制这个TAB的标签。先看代码:

    private void setIndicator(int icon, int title, int view) {
     
     View localView = LayoutInflater.from(this.tabhost.getContext()).inflate(R.layout.main_activity_tab, null);
     ((ImageView)localView.findViewById(R.id.main_activity_tab_image)).setBackgroundResource(icon);
     ((TextView)localView.findViewById(R.id.main_activity_tab_text)).setText(title);
     
     String str = getResources().getString(title);
     
     TabHost.TabSpec localTabSpec = tabhost.newTabSpec(str).setIndicator(localView).setContent(view);
     tabhost.addTab(localTabSpec);
     
    }

 

注意到这个main_activity_tab的layout了吧,看看它的内容:

 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"   
  3.     android:gravity="center"   
  4.     android:orientation="vertical"   
  5.     android:id="@id/tabsLayout"   
  6.     android:background="@drawable/tab_item_bkg"   
  7.     android:padding="3.0dip"   
  8.     android:layout_width="wrap_content"   
  9.     android:layout_height="wrap_content"   
  10.     android:layout_marginTop="3.0dip"   
  11.     android:layout_marginBottom="3.0dip"  
  12.   >  
  13.       
  14.     <FrameLayout   
  15.         android:layout_width="fill_parent"   
  16.         android:layout_height="fill_parent"   
  17.         android:layout_weight="0.6">  
  18.           
  19.         <ImageView   
  20.             android:layout_gravity="center"   
  21.             android:id="@id/main_activity_tab_image"   
  22.             android:layout_width="wrap_content"   
  23.             android:layout_height="wrap_content"   
  24.             android:layout_marginTop="2.0dip"   
  25.             android:scaleType="center" />  
  26.           
  27.     </FrameLayout>  
  28.       
  29.     <TextView   
  30.         android:textSize="12.0dip"   
  31.         android:textColor="@drawable/tab_text_selector"   
  32.         android:id="@id/main_activity_tab_text"   
  33.         android:layout_width="wrap_content"   
  34.         android:layout_height="wrap_content"   
  35.         android:text="Title" />  
  36. </LinearLayout  

 

这个文件定义了每个TAB标签的样式,tab_item_bkg定义每个item的背景(包括focused/selected/pressed),每个item上面的图标和下面的文字都在代码中动态定义即可。其中tab_text_selector则定义文字的颜色。

 

剩下的事情就越发明显了,不用多说了。

最后更新:2017-04-02 06:51:59

  上一篇:go 创新源于模仿之四:增强的ExpandableListView
  下一篇:go 创新源于模仿之三:实现左右两个屏幕的切换