0

我使用SherlockActionBar能夠在API> = 2.2上看到我的標籤頁我需要的是具有標籤的自定義樣式:背景,文本喜好和文本顏色。 當我測試一下我的2.3 API的手機上 - 選項卡的外觀,因爲我想: https://dl.dropboxusercontent.com/u/4277073/device-2013-06-20-105544.pngSherlockActionBar在tabhost中的標籤之間的白色差距

然而,當我測試了三星S3我得到在標籤之間怪異的白色間隙: https://dl.dropboxusercontent.com/u/4277073/tabs.png

你知道爲什麼它是這樣的? 或者因爲我沒有在我的應用程序中使用操作欄,我應該以不同方式實現選項卡導航?怎麼樣?

這裏是我的tabhost片段

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#EFEFEF"> 

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

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_alignParentBottom="true" 
     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" 
     android:layout_above="@android:id/tabs"> 

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

     <FrameLayout 
      android:id="@+id/tab_2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
     <FrameLayout 
      android:id="@+id/tab_3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 
    </FrameLayout> 
</RelativeLayout> 
</TabHost> 

這裏是我的標籤XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="@dimen/tab_height" 
android:gravity="center" 
android:padding="@dimen/tab_padding" 
android:layout_weight="1" 
android:layout_width="0dp" 
android:background="@drawable/tab_selector"> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="@dimen/text_small" 
    android:textStyle="bold" 
    android:textColor="@drawable/tab_text_selector" /> 
</LinearLayout> 

這是我TabsFragment,我更改選項卡的外觀在setTabColor()方法:

public class TabsFragment extends SherlockFragment implements OnTabChangeListener { 

private static final String TAG = "FragmentTabs"; 
public static final String TAB_FIND = "FIND FOOD"; 
public static final String TAB_MAP = "MAP"; 
public static final String TAB_EVENTS = "EVENTS"; 

private View mRoot; 
private TabHost mTabHost; 
private int mCurrentTab; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    mRoot = inflater.inflate(R.layout.tabs_fragment, null); 
    setHasOptionsMenu(true); 
    mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost); 
    setupTabs(); 
    return mRoot; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    setRetainInstance(true); 

    mTabHost.setOnTabChangedListener(this); 
    mTabHost.setCurrentTab(mCurrentTab); 
    // manually start loading stuff in the first tab 
    updateTab(TAB_FIND, R.id.tab_1); 
} 

private void setupTabs() { 
    mTabHost.setup(); // important! 
    mTabHost.addTab(newTab(TAB_FIND, R.string.tab_find, R.id.tab_1)); 
    mTabHost.addTab(newTab(TAB_MAP, R.string.tab_map, R.id.tab_2)); 
    mTabHost.addTab(newTab(TAB_EVENTS, R.string.tab_events, R.id.tab_3)); 
    setTabColor(mTabHost); 
} 

private TabSpec newTab(String tag, int labelId, int tabContentId) { 
    View indicator = LayoutInflater.from(getActivity()).inflate(
      R.layout.tab, 
      (ViewGroup) mRoot.findViewById(android.R.id.tabs), false); 
    ((TextView) indicator.findViewById(R.id.text)).setText(labelId);have 
    ((TextView) indicator.findViewById(R.id.text)).setTextSize(16); 

    TabSpec tabSpec = mTabHost.newTabSpec(tag); 
    tabSpec.setIndicator(indicator); 
    tabSpec.setContent(tabContentId); 
    return tabSpec; 
} 

@Override 
public void onTabChanged(String tabId) { 
    if (TAB_FIND.equals(tabId)) { 
     updateTab(tabId, R.id.tab_1); 
     mCurrentTab = 0; 

    } 
    if (TAB_MAP.equals(tabId)) { 
     updateTab(tabId, R.id.tab_2); 
     mCurrentTab = 1; 

    } 
    if (TAB_EVENTS.equals(tabId)) { 
     updateTab(tabId, R.id.tab_3); 
     mCurrentTab = 1; 

    } 

    setTabColor(mTabHost); 
    return; 
} 

public void setTabColor(TabHost tabhost) { 
    int color ; 
    TextView tv; 


    for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
    { 
     color = getSherlockActivity().getResources().getColor(R.color.grey_tab_light); 
     tabhost.getTabWidget().getChildAt(i).setBackgroundColor(color); //unselected 
     View v = tabhost.getTabWidget().getChildAt(i); 
     tv = ((TextView) v.findViewById(R.id.text)); 
     tv.setTextColor(Color.WHITE); 

    } 
    color = getSherlockActivity().getResources().getColor(R.color.grey_tab_dark); 
    tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(color); // selected 
    color = getSherlockActivity().getResources().getColor(R.color.orange_bright); 
    View v = tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()); 
    tv = ((TextView) v.findViewById(R.id.text)); 
    tv.setTextColor(color); 
} 

private void updateTab(String tabId, int placeholder) { 
    FragmentManager fm = getFragmentManager(); 

    if (TAB_FIND.equals(tabId)) { 
     if (fm.findFragmentByTag(tabId) == null) { 
      fm.beginTransaction().replace(placeholder, new FindFoodFragment(), tabId).commit(); 
     } 
    } 
    if (TAB_MAP.equals(tabId)) { 
     if (fm.findFragmentByTag(tabId) == null) { 
      fm.beginTransaction().replace(placeholder, new MapFragment(), tabId).commit(); 
     } 
    } 
    if (TAB_EVENTS.equals(tabId)) { 
     if (fm.findFragmentByTag(tabId) == null) { 

      fm.beginTransaction().replace(placeholder, new EventsFragment(), tabId).commit(); 

     } 
    } 
} 

} 

謝謝

回答

0

對我來說,仍然很奇怪,爲什麼在高分辨率屏幕上這是問題。

我解決它通過在TabWidget

<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_alignParentBottom="true" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/gray_light" /> 
指定背景顏色