我想更改Android中ActionBar
中各個標籤的顏色。那可能嗎?到目前爲止,我發現的所有文檔都表明,您可以整體更改ActionBar
的顏色,但不能更改個人tabs
。任何幫助是極大的讚賞。如何更改android中單個選項卡的顏色?
0
A
回答
1
試試這個
tabHost = getTabHost();
tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#FF0001"));
tabHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.parseColor("#FF0002"));
tabHost.getTabWidget().getChildAt(3).setBackgroundColor(Color.parseColor("#FF0003"));
.................................
0
你接頭主機XML文件
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">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<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">
</FrameLayout>
</LinearLayout>
</TabHost>
在主要活動
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
setupTab(new TextView(this), "Tab 1");
setupTab(new TextView(this), "Tab 2");
setupTab(new TextView(this), "Tab 3");
}
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.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
CustomTabsLayout tabs_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<TextView android:id="@+id/tabsText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Title"
android:textSize="15dip" android:textColor="@drawable/tab_text_selector" />
</LinearLayout>
tab_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@android:color/white" />
<item android:state_focused="true" android:color="@android:color/white" />
<item android:state_pressed="true" android:color="@android:color/white" />
<item android:color="#f8f8f8" />
</selector>
tab_bg_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<!-- Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>
tab_bg_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>
tab_bg_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#5C5C5C" android:centerColor="#424242"
android:endColor="#222222" android:angle="-90" />
</shape>
,最後在主活動類
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
並完成:)
相關問題
- 1. 更改Android中的選項卡顏色
- 2. 如何更改Android中以下3個選項卡的顏色?
- 3. Ionic2選項卡 - 如何更改選定選項卡的顏色?
- 4. 更改個別選項卡的顏色
- 5. 如何更改actionbarsherlock選項卡顏色和選項卡下的行的顏色?
- 6. 我如何更改android中選項卡的背景顏色?
- 7. 在Android中更改選項卡欄選定的選項卡顏色
- 8. 如何更改滑動選項卡中選定的選項卡顏色?
- 9. 如何更改在Android中刷卡選項卡標題顏色Android
- 10. 如何更改TabControl中選定選項卡的顏色?
- 11. 如何更改tablayout中選定選項卡的顏色?
- 12. 更改選項卡選擇選項卡時的文本顏色
- 13. Android:如何更改首選項中的選定項目顏色?
- 14. 試圖更改Android中的選項卡上的顏色
- 15. 如何更改選項卡控件選項卡項目的標題顏色[MahApps.Metro]
- 16. 更改「選項卡控件」顏色
- 17. Android - 如何更改動作選項卡菜單的文本顏色?
- 18. 我們是否可以更改Android中的選項卡顏色
- 19. 如何更改PhpStorm中活動選項卡的背景顏色?
- 20. 如何更改活動/選定選項卡的顏色?
- 21. 如何更改TabLayout選定選項卡的圖標顏色?
- 22. 我如何更改jtabbedpane選定的選項卡背景顏色
- 23. 如何爲ActionbarTab更改選定的選項卡顏色或字體顏色?
- 24. 更改自定義選項卡欄中每個選項卡的背景顏色
- 25. 單擊或選項卡上的表單字段顏色更改
- 26. 更改選項卡布局選項卡顏色?
- 27. 更改選項卡的顏色取決於選擇哪個選項卡
- 28. 如何在選擇時更改顏色選項卡
- 29. 更改tk選項菜單中單個單詞的顏色?
- 30. 如何更改禁用的選項卡控件的顏色
試試我的答案。你可以把你的顏色設置爲「#FF0001」。 – anuruddhika
你有答案嗎? – anuruddhika
對不起,我在發佈我的問題後不久就睡着了。我會盡快測試每個人提出的解決方案,並會以我的結果作出迴應。 – Patr3xion