閱讀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 創新源於模仿之三:實現左右兩個屏幕的切換