0

我使用 SherlockActionBar 能够在 API >=2.2 上查看我的选项卡我需要的是自定义选项卡样式:背景、文本喜好和文本颜色。当我在我的 2.3 API 手机上测试它时 - 选项卡看起来像我想要的那样: https ://dl.dropboxusercontent.com/u/4277073/device-2013-06-20-105544.png

但是,当我在三星 S3 上对其进行测试时,标签之间出现了奇怪的白色间隙: https ://dl.dropboxusercontent.com/u/4277073/tabs.png

你知道为什么会这样吗?或者因为我在我的应用程序中根本不使用操作栏,所以我应该以不同的方式实现标签导航?如何?

这是我的标签主机片段

<?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();

        }
    }   
}

}

谢谢

4

1 回答 1

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" />
于 2013-06-22T10:32:43.923 回答