Android的更改每个选项卡单独的背景颜色 [英] Android Change each tab individual background color

查看:584
本文介绍了Android的更改每个选项卡单独的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置三个选项卡,在每一个不同的颜色。这是看我打算。

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

下面是相关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);

请帮我,我AP preciate它。

Please help me out, I'd appreciate it.

P.S。对不起,tinypic链接,我不能直接发表图片(没有足够的声誉)

P.S. Sorry for tinypic links, I can't post images directly (not enough reputations)

编辑:我终于设法得到看看我去了!所以,任何人谁是寻找一个解决方案,以作为我有一个同样的问题,这里我是如何做的。

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.

我在的themes.xml自定义主题,我补充说:MyActionBar,以风格

I had a custom theme in themes.xml and I added "MyActionBar" to styles

    <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的风格,现在看起来是这样的:

"MyActionBar" style now looks like this:

    <!-- 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>

code你感兴趣的行是机器人:backgroundStacked。
我创建了颜色的PNG我需要(all_colors),并把它添加到backgroundStacked。

The line of code you are interested in is "android:backgroundStacked". I created a png with colors I needed ("all_colors") and added it to backgroundStacked.

这就是它。

这是结果:

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.

我在的themes.xml自定义主题,我补充说:MyActionBar,以风格

I had a custom theme in themes.xml and I added "MyActionBar" to styles

<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的风格,现在看起来是这样的:

"MyActionBar" style now looks like this:

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

code你感兴趣的行是机器人:backgroundStacked。我创建了颜色的PNG我需要(all_colors),并把它添加到backgroundStacked。

The line of code you are interested in is "android:backgroundStacked". I created a png with colors I needed ("all_colors") and added it to backgroundStacked.

这就是它。

这是结果:

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

这篇关于Android的更改每个选项卡单独的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆