【问题标题】:Android Change each tab individual background colorAndroid 更改每个选项卡的单独背景颜色
【发布时间】:2015-01-04 13:49:44
【问题描述】:

我正在尝试设置三个选项卡,每个选项卡都有不同的颜色。这就是我想要的样子。

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

以下是相关代码:

 // 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);

请帮帮我,我将不胜感激。

附:对不起,微小的链接,我不能直接发布图片(没有足够的声誉)

编辑:我终于设法得到我想要的样子!因此,对于任何正在寻找与我相同的问题的解决方案的人,我就是这样做的。

我在 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”样式现在看起来像这样:

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

您感兴趣的代码行是“android:backgroundStacked”。 我用我需要的颜色(“all_colors”)创建了一个 png 并将其添加到 backgroundStacked。

就是这样。

这是结果:

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

【问题讨论】:

  • 您应该将您的解决方案作为答案提交,然后接受它,这样问题就不会显得没有答案。
  • 会这样做,谢谢你的提示。我还没有习惯stackoverflow:D

标签: android colors tabs


【解决方案1】:

我终于找到了我想要的样子!因此,对于正在寻找与我遇到的相同问题的解决方案的任何人,我就是这样做的。

我在 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 name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#f5f5f5</item>
<item name="android:backgroundStacked">@drawable/all_colors</item>

您感兴趣的代码行是“android:backgroundStacked”。我用我需要的颜色(“all_colors”)创建了一个 png 并将其添加到 backgroundStacked。

就是这样。

这是结果:

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

【讨论】:

  • 如何将它添加到你在这里没有提到的操作栏我已经创建了带有标签的 viewpager 现在我想更改标签的背景怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多