ActionBar AppCompat更改选项卡指示器颜色 [英] ActionBar AppCompat change Tab indicator color
问题描述
我已经看过很多帖子和答案,但仍然不清楚,如果我可以更改Tab指示器的颜色而不使用Android Asset Studio
I have seen many posts and answers but still not clear to me, what to do if i have to change Tab indicator color without using Android Asset Studio
public class MainActivity extends ActionBarActivity implements android.support.v7.app.ActionBar.TabListener{
private ViewPager tabsviewPager;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabsviewPager = (ViewPager) findViewById(R.id.tabspager);
mTabsAdapter = new TabsAdapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));
getSupportActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
//getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
getSupportActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab friendstab = getSupportActionBar().newTab().setIcon(R.drawable.ic_action_action_account_child).setTabListener(this);
Tab publicprofiletab = getSupportActionBar().newTab().setIcon(R.drawable.ic_action_action_account_box).setTabListener(this);
Tab communitytab = getSupportActionBar().newTab().setIcon(R.drawable.ic_action_action_account_circle).setTabListener(this);
getSupportActionBar().addTab(friendstab);
getSupportActionBar().addTab(publicprofiletab);
getSupportActionBar().addTab(communitytab);
我已附上了现有应用的屏幕截图,目前我的操作栏外观如何,见下文:
I have attached existing app screenshot, how my action bar currently looking, see below:
如您所见,我正在为操作栏background
使用#0000ff
,并且我想为Tab Indicator
使用相同的颜色
As you can see, i am using #0000ff
for Action bar background
, and same color i want to use for Tab Indicator
推荐答案
actionBarTabStyle
属性应具有基本的Application/Activity主题,而不是ActionBar
样式
actionBarTabStyle
attribute should be with the base Application/Activity theme , not under ActionBar
style
请参见为您的应用程序创建新样式并应用于清单文件中的节点.
<style name="AppTheme" parent="Theme.Base.AppCompat">
<item name="android:actionBarStyle">@style/MyCustomActionBar</item>
<item name="android:actionBarTabStyle">@style/MyActionBarTabViewStyle</item>
<item name="actionBarTabStyle">@style/MyActionBarTabView</item>
像这样的自定义ActionBarTab主题
<style name="MyActionBarTabViewStyle" parent="Widget.AppCompat.Base.ActionBar.TabView">
<item name="android:background">@drawable/action_bar_stacked_background</item>
和 @ style/MyActionBarTabView ,这是一种较为简单的android样式.
and @style/MyActionBarTabView this is simpler one android style.
另请参阅样式化ActionBar
这篇关于ActionBar AppCompat更改选项卡指示器颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!