以编程方式更改动作条标签颜色 [英] Change ActionBar tab color programmatically
问题描述
我在我的应用程序下面的动作条选项卡。我想知道什么是改变周围的颜色,以配合我的应用程序的最佳途径。
I have the following ActionBar tab in my app. I was wondering what is the best way to change the colors around to match my app.
- 每个标签都有一个不同的背景内容。如何添加 不同的背景颜色为每个标签?
- 如何改变淡蓝色条的颜色为白色,给它一个3D 看?
- Each tab has a different background for the contents. How do I add separate background colors for each tab?
- How do I change the light blue strip color to white to give it a 3D look?
我看到下面的code:
I saw the following code:
ActionBar ab = getActionBar();
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
但对于所有的选项卡,线条改变颜色,只是一种颜色。
But that line changes color for all the tabs to just one color.
标签code。在我的应用程序是:
The Tab code in my app is:
ActionBar ab = getActionBar();
//ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff"))); not changing the tab color
//ab.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
Tab tab = ab.newTab()
.setText( "TY1" )
.setTabListener(
new MyTabListener( this, TY1.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "TY2" )
.setTabListener(
new MyTabListener( this, TY2.class.getName() ) );
ab.addTab( tab );
tab = ab.newTab()
.setText( "ty3" )
.setTabListener(
new MyTabListener( this, TY3.class.getName() ) );
ab.addTab( tab );
任何及所有的帮助是AP preciated。我可以使用XML为好,如果有人点我在正确的方向。
Any and all help is appreciated. I can use XML as well, if someone points me in the right direction.
推荐答案
您可以设置自定义视图为每个标签。创建标签一个新的布局资源(它可以只是一个TextView)。离开它的背景空,使九补丁用于绘制选择指示符。找一个 LayoutInflater
用
You can set a custom View for each tab. Create a new layout resource for the tab (it can just be a TextView). Leave its background empty and make a nine-patch drawable for the selection indicator. Get a LayoutInflater
using
LayoutInflater inflater = getSystemService(LAYOUT_INFLATER_SERVICE);
然后为每个选项卡,你可以这样做:
Then for each tab, you can do this:
Tab tab = ab.newTab()
.setText("TY1")
.setTabListener(new MyTabListener(this, TY1.class.getName()));
View tabView = inflater.inflate(R.layout.my_tab_layout, null);
tabView.setBackgroundColor(...); // set custom color
tab.setCustomView(tabView);
ab.addTab(tab);
这篇关于以编程方式更改动作条标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!