gpt4 book ai didi

Android更改每个选项卡的单独背景颜色

转载 作者:行者123 更新时间:2023-11-29 17:45:51 26 4
gpt4 key购买 nike

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

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

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

附言很抱歉 tinypic 链接,我不能直接发布图片(没有足够的声誉)

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

我在 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

最佳答案

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

我在 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

关于Android更改每个选项卡的单独背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com