简体   繁体   English

Android更改每个标签的个别背景颜色

[英]Android Change each tab individual background color

I am trying to setup three tabs,each in a different color. 我正在尝试设置三个选项卡,每个选项卡都有不同的颜色。 This is the look i am going for. 这就是我想要的样子。

http://i60.tinypic.com/261ff5u.png http://i60.tinypic.com/261ff5u.png

Here is the relevant code: 这是相关代码:

 // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    ActionBar.Tab tab = actionBar.newTab()
            .setText("Tab1")
            .setTabListener(
                    new MyTabListener(this, UrgentFragment.class.getName()));
    View tabView = inflater.inflate(R.layout.ab_red, null);
    tabView.setBackgroundResource(R.drawable.red); // set custom color
    tab.setCustomView(tabView);
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText("Tab2")
            .setTabListener(
                    new MyTabListener(this, ImportantFragment.class.getName()));
    View tabView2 = inflater.inflate(R.layout.ab_yellow, null);
    tabView2.setBackgroundResource(R.drawable.yellow); // set custom color
    tab.setCustomView(tabView2);
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText("Tab3")
            .setTabListener(
                    new MyTabListener(this, InfoFragment.class.getName()));
    View tabView3 = inflater.inflate(R.layout.ab_green, null);
    tabView3.setBackgroundResource(R.drawable.green); // set custom color
    tab.setCustomView(tabView3);
    actionBar.addTab(tab);

Please help me out, I'd appreciate it. 请帮帮我,我很感激。

PS Sorry for tinypic links, I can't post images directly (not enough reputations) PS对不起,有点类型的链接,我不能直接发布图片(没有足够的声誉)

EDIT: I finally managed to get the look I was going for! 编辑:我终于设法得到了我想要的样子! So, to anyone who is looking for a solution to a same problem as I had, here is how I did it. 所以,对于那些正在寻找解决同一问题的人来说,我就是这样做的。

I had a custom theme in themes.xml and I added "MyActionBar" to styles 我在themes.xml中有一个自定义主题,我在样式中添加了“MyActionBar”

    <style name="CustomActionBarTheme"
    parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:radioButtonStyle">@style/RadioButtonRadioRed</item>
    <item name="android:listChoiceIndicatorSingle">@drawable/radiored_btn_radio_holo_light</item>
</style>

"MyActionBar" style now looks like this: “MyActionBar”样式现在看起来像这样:

    <!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#f5f5f5</item>
    <item name="android:backgroundStacked">@drawable/all_colors</item>
</style>

The line of code you are interested in is "android:backgroundStacked". 您感兴趣的代码行是“android:backgroundStacked”。 I created a png with colors I needed ("all_colors") and added it to backgroundStacked. 我用我需要的颜色(“all_colors”)创建了一个png并将其添加到backgroundStacked中。

That's it. 而已。

This is the result: 这是结果:

http://i59.tinypic.com/9h50rt.png http://i59.tinypic.com/9h50rt.png

I finally managed to get the look I was going for! 我终于设法得到了我想要的样子! So, to anyone who is looking for a solution to a same problem as I had, here is how I did it. 所以,对于那些正在寻找解决同一问题的人来说,我就是这样做的。

I had a custom theme in themes.xml and I added "MyActionBar" to styles 我在themes.xml中有一个自定义主题,我在样式中添加了“MyActionBar”

<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:radioButtonStyle">@style/RadioButtonRadioRed</item>
<item name="android:listChoiceIndicatorSingle">@drawable/radiored_btn_radio_holo_light</item>

"MyActionBar" style now looks like this: “MyActionBar”样式现在看起来像这样:

<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#f5f5f5</item>
<item name="android:backgroundStacked">@drawable/all_colors</item>

The line of code you are interested in is "android:backgroundStacked". 您感兴趣的代码行是“android:backgroundStacked”。 I created a png with colors I needed ("all_colors") and added it to backgroundStacked. 我用我需要的颜色(“all_colors”)创建了一个png并将其添加到backgroundStacked中。

That's it. 而已。

This is the result: 这是结果:

http://i59.tinypic.com/9h50rt.png http://i59.tinypic.com/9h50rt.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM