2012-11-20 55 views
1

我正在製作一個android應用程序,並且我在我的xml中有一個tabhost和兩個選項卡。我打電話給他們,他們工作得很好。但我想擺脫標籤上醜陋的灰色/橙色。我曾嘗試使用圖像設置bg。我用這個代碼:在eclipse中設置選項卡背景

th.getTabWidget().setBackgroundResource(R.drawable.tab_normal); 

但它顯示是這樣的:

Wrong bg

我怎樣才能得到它替換灰色和橙色的顏色嗎?

感謝

+2

你是爲了一種享受。我無法準備好你將要進入的地獄,這是多麼可怕的選項卡在Android上。每個製造商和每個Android版本的風格都不相同。這也是爲什麼現在舊標籤已被棄用的原因......並且您應該通過使用actionbarsherlock或類似工具開始使用新標籤。哦,我也忘了一些功能不能在某些版本的Android上設計,除非你做了一些可能會破壞的危險代碼:S – Warpzit

+0

好的,謝謝:P我也在我的應用中使用ABS(action bar sherlock)。但我希望標籤只能從中間和下來。而不是採取完整的佈局。再次感謝。 –

+0

你是什麼意思只從中間和下來? – Warpzit

回答

1

步驟來完全自定義tabwidget:

  1. 爲tabwidget創建自定義佈局:此佈局相似默認的一個由一個Icon和一個組成。你可以選擇你想要的任何佈局。請注意,父佈局具有有狀態背景tabwidget_selector。這個drawable是取代默認橙色焦點顏色的關鍵。你可以選擇你自己的焦點顏色。

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:background="@drawable/tabwidget_selector" 
    android:gravity="center" 
    android:orientation="vertical" > 
    
    <ImageView 
        android:id="@+id/tabIndicatorIcon" 
        android:layout_width="28dp" 
        android:layout_height="28dp" 
        android:layout_marginTop="2dp" /> 
    
    <TextView 
        android:id="@+id/tabIndicatorText" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="2dp" 
        android:textColor="@color/white" /> 
    

  2. 寫函數返回tabwidget視圖。設置文本,圖標繪製你的tabwidget

    private View getTabIndicatorView(Context context, String tag, int drawable) { 
        View view = LayoutInflater.from(context).inflate(
          R.layout.tab_widget_custom, null); 
        TextView tv = (TextView) view.findViewById(R.id.tabIndicatorText); 
        tv.setText(tag); 
        ImageView tabIndicatorIcon = (ImageView) view 
          .findViewById(R.id.tabIndicatorIcon); 
        tabIndicatorIcon.setBackgroundResource(drawable); 
        return view; 
    
    } 
    
  3. 使用此功能在您的活動:

TabHost.TabSpec setIndicator(查看視圖)指定一個視圖選項卡 指標。

 Intent intent; 
     TabHost.TabSpec spec; 

     spec = tabHost.newTabSpec("Inbox").setIndicator(
       getTabIndicatorView(MainTabActivity.this, "Hộp thư", 
         R.drawable.tin_nhan_tab_selector)); 
     intent = new Intent(this, xxxx.InboxActivity.class); 

     spec.setContent(intent); 
     tabHost.addTab(spec); 
+0

謝謝,這幫助了我很多。 –

0

你可以有tabwidget背景:

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<RelativeLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 

     <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_alignParentBottom="true" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/tabbackground" 
     android:layout_margin="7dp" 

     /> 


     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_above="@android:id/tabs" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_margin="5dp" 
      android:background="@drawable/list_shape"> 
     </FrameLayout> 
</RelativeLayout>