简体   繁体   中英

Changing Android Actionbar Title Color

I cannot seem to figure out why I cannot change the text color of the action bar text..

I have been trying a number of style combinations, and I don't seem to see why one of them has not worked yet.

I was using a custom font and thought that perhaps it had something to do with the typeface but I have since tried changing the color of the native font. Below you can find where I currently stand, I have tried a number of layered styles but to no avail. Has anyone run into this issue?

Here is my most current attempt at changing the action bar text color

<resources>
<style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/teal_500</item>
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>

<style name="ActionBarStyle"
       parent="Widget.AppCompat.ActionBar">
    <item name="android:background">@color/teal_500</item>
    <item name="android:titleTextStyle">@style/ActionBarTitleColor</item>
</style>

<style name="ActionBarTitleColor" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
    <!-- Customize your theme here. -->
    <item name="android:textColor">@color/teal_50</item>
</style>
</resources>

Here is the main activity

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    configureTitle();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void configureTitle(){
   // SpannableString s = new SpannableString("Photoswap");
    //s.setSpan(new TypefaceSpan(this, "Pacifico.ttf"), 0, s.length(),
           // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Update the action bar title with the TypefaceSpan instance
  //  ActionBar actionBar = getSupportActionBar();
   // actionBar.setTitle(s);


}

}

STEP 1: Grab the ID for the action_bar_title

int titleId = getResources().getIdentifier("action_bar_title", "id", "android");

STEP 2: Use TextView

TextView abTitle = (TextView) findViewById(titleId);
abTitle.setTextColor(colorId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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