ActionBar text color

后端 未结 24 2593
日久生厌
日久生厌 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:18

    Add it to the root of the action bar. I had this problem.

    <style name="ActionBar" parent="@style/Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
        <item name="actionMenuTextColor">@color/stdDarkBlueText</item>
    </style>
    
    <style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
        <item name="titleTextStyle">@style/ActionBarTitleText</item>
        <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
    </style>
    
    <style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/stdDarkBlueText</item>
        <item name="android:textSize">12sp</item>
    </style>
    
    <style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
        <item name="android:textColor">@color/stdDarkBlueText</item>
        <item name="android:textSize">12sp</item>
    </style>
    

    actionMenuTextColor - changes text color for the action bar text, it never worked when it was part of the Widget.Styled.ActionBar, so I had to add it to the root. Other 2 attributes change title and subtitle of an action bar.

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

    I found a way that works well with any flavor of ActionBar (Sherlock, Compat, and Native):

    Just use html to set the title, and specify the text color. For example, to set the ActionBar text color to red, simply do this:

    getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));
    

    You can also use the red hex code #FF0000 instead of the word red. If you are having trouble with this, see Android Html.fromHtml(String) doesn't work for <font color='#'>text</font>.


    Additionally, if you want to use a color resource, this code can be used to get the correct HEX String, and removing the alpha if needed (the font tag does not support alpha):

    int orange = getResources().getColor(R.color.orange);
    String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));
    
    0 讨论(0)
  • 2020-11-22 06:20

    It's an old topic but for future readers, using ToolBar makes everything very easy:

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    toolbar.setTitle(R.string.app_name);
    toolbar.setTitleTextColor(getResources().getColor(R.color.someColor));
    setSupportActionBar(toolbar);
    
    0 讨论(0)
  • 2020-11-22 06:20

    you can change color of any text by use html <font> attribute directly in xml files. for example in strings.xml :

    <resources>
        <string name = "app_name">
            <html><font color="#001aff">Multi</font></html>
            <html><font color="#ff0044">color</font></html>
            <html><font color="#e9c309">Text </font></html>
        </string>
    </resources>
    

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

    The ActionBar ID is not available directly, so you have to do little bit of hacking here.

    int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    if (actionBarTitleId > 0) {
        TextView title = (TextView) findViewById(actionBarTitleId);
        if (title != null) {
            title.setTextColor(Color.RED);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:23

    The most straight-forward way is to do this in the styles.xml.

    Google's template styles.xml currently generates the following:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    If you add one more line before the closing tag, as shown, that will change the text color to what it should be with a Dark ActionBar:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- 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.AppCompat.Dark.ActionBar</item>
    </style>
    

    If you want to customize the color to something else, you can either specify your own color in colors.xml or even use a built-in color from Android using the android:textColorPrimary attribute:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- 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/AppTheme.AppBarOverlay</item>
    </style>
    
    
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@android:color/darker_gray</item>
    </style>
    

    Note: This changes the color of the title and also the titles of any MenuItems displayed in the ActionBar.

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