ActionBar text color

后端 未结 24 2594
日久生厌
日久生厌 2020-11-22 05:31

how can I change the text color of the ActionBar? I\'ve inherited the Holo Light Theme, I\'m able to change the background of the ActionBar but I don\'t find out what is the

相关标签:
24条回答
  • 2020-11-22 06:23

    If you want to style the subtitle also then simply add this in your custom style.

    <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    

    People who are looking to get the same result for AppCompat library then this is what I used:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="CustomActivityTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
            <item name="actionBarStyle">@style/MyActionBar</item>
            <!-- other activity and action bar styles here -->
        </style>
    
        <!-- style for the action bar backgrounds -->
        <style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="android:background">@drawable/actionbar_background</item>
            <item name="background">@drawable/actionbar_background</item>
            <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
            <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
         </style>
        <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">@color/color_title</item>
          </style>
    </resources>
    
    0 讨论(0)
  • 2020-11-22 06:23

    This is not the recommended solution as I am going in android apis here but as my application requires to change the theme dynmically on conditions xml not possible here, So I need to do this. But This solution is working very nice.

    Solution:--

     /**
     * 
     * @author Kailash Dabhi
     * @email kailash09dabhi@gmail.com
     *
     */ 
     public static void setActionbarTextColor(Activity activity, int color) {
        Field mActionViewField;
        try {
            mActionViewField = activity.getActionBar().getClass()
                    .getDeclaredField("mActionView");
            mActionViewField.setAccessible(true);
            Object mActionViewObj = mActionViewField.get(activity
                    .getActionBar());
    
            Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
                    "mTitleView");
            mTitleViewField.setAccessible(true);
            Object mTitleViewObj = mTitleViewField.get(mActionViewObj);
    
            TextView mActionBarTitle = (TextView) mTitleViewObj;
            mActionBarTitle.setTextColor(color);
            // Log.i("field", mActionViewObj.getClass().getName());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:24

    A nice solution is to user a SpannableStringBuilder and set the color that way. You can even use different colors on different parts of the string, add images etc.

    Tested with the new support library.

    See Android: Coloring part of a string using TextView.setText()?

    0 讨论(0)
  • 2020-11-22 06:24

    Here Style.xml is like

     <resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="actionBarTheme">@style/MyTheme</item>
    </style>
    
    
     <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/AppTheme.ActionBarStyle</item>
    </style>
    
    <style name="AppTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/AppTheme.ActionBar.TitleTextStyle</item>
    </style>
    
    <style name="AppTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/colorBlack</item>
    </style>
    

    You should add

    <item name="actionBarTheme">@style/MyTheme</item> 
    

    in AppTheme

    0 讨论(0)
  • 2020-11-22 06:28

    This is the solution I used after checking all answers

    <!-- Base application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorAccent">@color/Button_color</item>
        <item name="android:editTextStyle">@style/EditTextStyle</item>
        <item name="android:typeface">monospace</item>
        <item name="android:windowActionBar">true</item>
        <item name="colorPrimary">@color/Title_Bar_Color</item>
        <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
        <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    
    </style>
    
    <style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>
    
    <style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="titleTextStyle">@style/ActionBarTitleText</item>
        <item name="android:background">@color/Title_Bar_Color</item>
        <item name="background">@color/Title_Bar_Color</item>
        <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
    </style>
    
    <style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/solid_white</item>
        <item name="android:textSize">12sp</item>
    </style>
    
    <style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
        <item name="android:textColor">@color/solid_white</item>
        <item name="android:textSize">12sp</item>
    </style>
    

    Change the required colors it will work

    0 讨论(0)
  • 2020-11-22 06:29

    A lot of answers are deprecated so I'll update the thread and show how to change text color and backgroundcolor on ActionBar (Toolbar) and in ActionBar pop up menu.

    The latest approach in Android (as of May 2018) in working with Action bar (Toolbar) is to use Theme with NoActionBar and use Toolbar instead.

    so here is what you need:

    1. In Activity (which extends AppCompatActivity) declare Toolbar:

      Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(myToolbar);
      
    2. Add Toolbar in the Activity's layout xml. Here we will set our custom (overwritten themes), note android:theme="@style/ThemeOverlay.AppTheme.ActionBar" and app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu", they will do the trick.

      <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?attr/actionBarSize"
      android:theme="@style/ThemeOverlay.AppTheme.ActionBar"
      app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu"
      app:layout_constraintTop_toTopOf="parent" />
      
    3. In your styles.xml you will have to overwrite those two styles to set custom colors:

      <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
      <item name="actionBarTheme">@style/ThemeOverlay.AppTheme.ActionBar</item>
      <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item>
      </style>
      
      <style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
      <item name="android:textColorPrimary">@color/action_bar_text_color</item>
      <item name="android:background">@color/action_bar_bg_color</item>
      </style>
      
      <style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
      <item name="android:background">@color/popup_bg_color</item>
      <item name="android:textColorPrimary">@color/popup_text_color</item>
      </style>
      

    That's it folks

    p.s. if you ask me I would say: YES, this whole Theme thing is a hell of a mess.

    0 讨论(0)
提交回复
热议问题