问题
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:

As you can see, i am using #0000ff
for Action bar background
, and same color i want to use for Tab Indicator
回答1:
actionBarTabStyle
attribute should be with the base Application/Activity theme , not under ActionBar
style
see create new style for your application and apply to node in manifest file.
<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>
custom ActionBarTab theme like this
<style name="MyActionBarTabViewStyle" parent="Widget.AppCompat.Base.ActionBar.TabView">
<item name="android:background">@drawable/action_bar_stacked_background</item>
and @style/MyActionBarTabView this is simpler one android style.
also refer Styling ActionBar
回答2:
Use a selector for the tabs and add presses state with drawables like:
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
For detailed answer and copyright links see this question and answer.
来源:https://stackoverflow.com/questions/27394531/actionbar-appcompat-change-tab-indicator-color