2012-04-12 17 views
2

在我活動我這樣做來設置TabHost:地帶未按

//...  
     mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
     mTabHost.getTabWidget().setDividerDrawable(R.drawable.my_divider_tab); 
     mTabHost.getTabWidget().setStripEnabled(true); 
     mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab); 
     mTabHost.getTabWidget().setRightStripDrawable(R.drawable.my_strip_tab); 
     setupTab(new TextView(this), getString(R.string.device_text_tab)); 
     setupTab(new TextView(this), getString(R.string.sensor_text_tab)); 
     setupTab(new TextView(this), getString(R.string.actuator_text_tab)); 
//... 

private void setupTab(final View view, final String tag) { 

    View tabview = createTabView(mTabHost.getContext(), tag); 
     TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
     public View createTabContent(String tag) {return view;} 
    }); 
    mTabHost.addTab(setContent); 
} 

private static View createTabView(final Context context, final String text) { 

    View view = LayoutInflater.from(context).inflate(R.layout.state_tabwidget, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 
    return view; 
} 

my_strip_tab.xml只是一個長方形:

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <solid 
     android:color="#FFFFFF"/> 

    <size 
     android:width="0dp" 
     android:height="2dp"/> 
</shape> 

垂直分隔線被繪製。但標籤下方的條未顯示...

可能是什麼問題?大小my_strip_tab

我剛剛發現:

/** 
* Controls whether the bottom strips on the tab indicators are drawn or 
* not. The default is to draw them. If the user specifies a custom 
* view for the tab indicators, then the TabHost class calls this method 
* to disable drawing of the bottom strips. 
* @param stripEnabled true if the bottom strips should be drawn. 
*/ 

這裏:TabWidgetExample

所以好像當我們使用自定義視圖選項卡(就像我現在這樣),鋼帶自動禁用... 如何啓用它?

回答

1

,我找到了解決這個問題的唯一解決辦法是:

//... 
<TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 


     <View 
      android:id="@+id/my_strip_tab" 
      android:layout_width="fill_parent" 
      android:layout_height="3dp" 
      android:background="@drawable/my_strip_tab"/> 

//... 

    </FrameLayout> 

my_strip_tab.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <solid 
     android:color="#67E667"/> 

</shape> 
+0

我遇到了同樣的問題。我解決了它1)移動我的setStrip行下面設置我的標籤的東西和2)我的drawable沒有指定大小。現在運行良好,感謝amp和@brian wang – kyle 2014-09-26 15:48:53

2

你所要做的唯一事情是移動代碼

此塊
mTabHost.getTabWidget().setStripEnabled(true); 
    mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab); 
    mTabHost.getTabWidget().setRightStripDrawable(R.drawable.my_strip_tab); 

之後所有setupTab()

原因是,如果您查看Google文檔,是在爲選項卡指示器使用自定義視圖時,將自動調用setStripEnabled(false)。您只需在設置所有自定義視圖或選項卡指示符後重新啓用它。