简体   繁体   English

如何以编程方式更改选项卡指示器颜色

[英]How to change Tab Indicator color programmatically

I am new to Android , and before starting programming i found that now a days many of the apps are using Fragments, especially Tab with Swipeable Views 我是Android的新手 ,在开始编程之前我发现现在很多应用程序都在使用Fragments,特别是带有Swipeable Views的Tab

How to change Tab Indicator/highlight color (I googled and changed ActionBar color to RED programmatically), but don't know how to change Tab Indicator color to RED. 如何更改选项卡指示器/突出显示颜色 (我用Google搜索并将ActionBar颜色更改为RED编程),但不知道如何将选项卡指示器颜色更改为红色。 (priority programmatically) (以编程方式优先)

still my ActionBar looks like this 我的ActionBar看起来像这样

I am using below lines to change background color of ActionBar , but i also need to change the color of Tab Indicator programmatically. 我使用下面的行来改变ActionBar的背景颜色 ,但我还需要以编程方式更改Tab Indicator的颜色。

actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.RED));

您可以通过创建自定义选项卡指示器视图并使用setIndicator为选项卡实现不同的不同指示器来实现。

You'll want to chain 你想要连锁

setIcon(R.drawable.ic_icon_name_here)

for your tabs to have icons 为您的标签提供图标

For the tab colors you'll want to use a custom style, for more info on those solutions take a look at How do I change the background of an Android tab widget? 对于要使用自定义样式的选项卡颜色,有关这些解决方案的更多信息,请查看如何更改Android选项卡窗口小部件的背景?

The one and the best way to change selector color is to use Styles (I saw "Please Note", btw). 改变选择器颜色的唯一方法是使用样式(我看到“请注意”,顺便说一句)。

In drawable folder create tab_selector.xml and do something like this: drawable文件夹中创建tab_selector.xml并执行以下操作:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/tab_selected_pink" />
    <item android:state_selected="false" android:drawable="@drawable/tab_unselected_white" />
</selector>

and then in your values/styles.xml I guess, do something like this: 然后在你的values/styles.xml我想,做这样的事情:

<style name="AppTheme" parent="Theme.AppCompat.Light">
     <item name="android:actionBarTabBarStyle">@style/MyTabStyle</item>
</style>

<style name="MyTabStyle" parent="android:Widget.ActionBar.TabBar">
    <item name="android:background">@drawable/tab_indicator_selector</item>
</style>

I'm possibly wrong with item name attribute in first style and parent attribute in second style. 我可能错误的第一个样式中的item name attribute和第二个样式中的parent attribute But in common it's will look like this. 但总的来说,它看起来会像这样。

As you can see it is easy to do. 你可以看到它很容易做到。 All that you really have to do is make 9patch drawables, if you want to support different screens. 如果你想支持不同的屏幕,你真正需要做的就是制作9patch drawables。

Also you can look at Jake Wharton's ViewPagerIndicator that's is most flexible way to use any Navigation Mode. 您还可以查看Jake Wharton的ViewPagerIndicator ,这是使用任何导航模式最灵活的方式。

If you know how many tabs you will have it is quite easy to change the icon color programmatically for each one or only a few of them. 如果您知道有多少个选项卡,则可以很容易地以编程方式为每个选项卡或仅少数选项卡更改图标颜色。

All you need to do is copy your existing drawable file which is used for your tab, give it a name and then change the color within it. 您需要做的就是复制用于选项卡的现有可绘制文件,为其命名,然后更改其中的颜色。

Then all you need to do is get each tab and set the drawable you wish to use as the icon. 然后,您需要做的就是获取每个选项卡并设置您希望用作图标的drawable。

tabLayout.setupWithViewPager(pager, true);
tabLayout.getTabAt(0).setIcon(R.drawable.tab_color_grey);
tabLayout.getTabAt(1).setIcon(R.drawable.tab_color_black);
tabLayout.getTabAt(2).setIcon(R.drawable.tab_color_black);
tabLayout.getTabAt(3).setIcon(R.drawable.tab_color_black);
tabLayout.getTabAt(4).setIcon(R.drawable.tab_color_gold);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM