如何使用这种代码在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 中创建选项卡。我只是想知道这种代码是否可能。谢谢。
最佳答案
您是否尝试通过从 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 设备,您需要设置两次。要使用新主题,您还需要将以下内容添加到 list 文件的应用程序标记中。
android:theme="@style/Theme.test"
关于android - android中如何设置导航选项卡的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17438966/