【问题标题】:How to change color of individual tabs in android?如何更改android中各个选项卡的颜色?
【发布时间】:2014-04-04 06:52:38
【问题描述】:

我想更改 Android 中 ActionBar 中各个选项卡的颜色。那可能吗?到目前为止,我发现的所有文档都表明您可以整体更改 ActionBar 的颜色,但不能更改单个 tabs 的颜色。非常感谢任何帮助。

【问题讨论】:

  • 试试我的答案。您可以根据需要将颜色设置为“#FF0001”。
  • 你得到答案了吗?
  • 抱歉,我在发布问题后不久就睡着了。我将很快测试每个人提出的解决方案,并用我的结果做出回应。

标签: android colors tabs android-actionbar


【解决方案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"));
.................................

【讨论】:

  • 是否可以为每个标签单独添加颜色?
  • 你能看看这个帖子http://stackoverflow.com/questions/26902549/add-a-different-color-for-each-action-bar-tabs-separately
【解决方案2】:

您的标签主机 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);

然后完成:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    相关资源
    最近更新 更多