【问题标题】:How to set the background color of navigation tab in android如何在android中设置导航选项卡的背景颜色
【发布时间】:2013-07-03 03:00:47
【问题描述】:

如何使用这种代码在android中设置导航选项卡的背景颜色。 `

            ActionBar bar = getSupportActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

            ActionBar.Tab tab1 = bar.newTab();
            ActionBar.Tab tab2 = bar.newTab();
            tab1.setText("Fragment A");
            tab2.setText("Fragment B");
            tab1.setTabListener(new MyTabListener());
            tab2.setTabListener(new MyTabListener());
            bar.addTab(tab1);
            bar.addTab(tab2);`

我没有在 xml 中创建选项卡。我只是想知道它是否可能在这种代码中。谢谢你。

【问题讨论】:

标签: android


【解决方案1】:

您是否尝试通过从 res/values/themes.xml 更改标签栏样式来设置标签背景样式?这是 Theme.Sherlock.Light 主题的示例。

<style name="Theme.test" parent="@style/Theme.Sherlock.Light">
        <item name="android:actionBarTabBarStyle">@style/Theme.test.tabbar.style</item>
        <item name="actionBarTabBarStyle">@style/Widget.test.ActionBar.TabBar</item>
</style>

<style name="Theme.test.tabbar.style" parent="@style/Theme.Sherlock.Light.ActionBar">
    <item name="android:background">#00ff00</item>
    <item name="background">#00ff00</item>
</style>

<style name="Widget.test.ActionBar.TabBar" parent="Widget.Sherlock.Light.ActionBar.TabBar">
    <item name="android:background">#00ff00</item>
    <item name="background">#00ff00</item>
</style>

由于 HoneyComb 和 pre-HoneyComb 设备,您需要设置两次。要使用新主题,您还需要将以下内容添加到清单文件中的应用程序标记中。

android:theme="@style/Theme.test"

【讨论】:

    【解决方案2】:

    我会先查看另一个 SO 帖子:Android ActionBar Tab Color 但是,您似乎只能设置底栏的整个背景颜色和颜色,而不是每个选项卡的颜色。

    否则,如果您想走老路,您可以使用 TabHost 而不是 ActionBar。这将允许您为每个特定选项卡设置颜色,并且您应该能够为它们设置类似于非常 4.2ish ActionBar 的样式。 '这很简单:

    tabHost.getTabWidget().getChildAt(TAB_1_INDEX).setBackgroundResource(R.drawable.tab_1_inactive);
    tabHost.getTabWidget().getChildAt(TAB_2_INDEX).setBackgroundResource(R.drawable.tab_2_inactive);
    tabHost.getTabWidget().getChildAt(TAB_3_INDEX).setBackgroundResource(R.drawable.tab_3_inactive);
    tabHost.getTabWidget().getChildAt(pos).setBackgroundResource(activeTabShape[pos]);
    

    【讨论】:

    • 我实际上打算将整个背景设置为我的标签。不是每个项目/标签。 :)
    • 啊酷,那么我引用的那个 SO 页面有很多很好的资源。你应该检查一下,然后投票给你喜欢的那个。 ;)
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多