2012-11-20 53 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>