我使用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
你知道为什么会这样吗? 或者因为我在我的应用程序中根本不使用动作栏,所以我应该以不同的方式实现标签导航?如何?
这是我的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 :(得分: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" />