Can't change the text color with Android Action Bar drop-down navigation

我们两清 提交于 2019-11-30 01:00:59

Please forgive my necromancy but this really bugged me today and the solution is actually quite quick and requires no custom adapters or styling.

If you change the context in the SpinnerAdapter's constructor to use getActionBar().getThemedContext() rather than this it will show light text on a dark background that will match the Action Bar's style.

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getActionBar().getThemedContext(), R.array.action_list, android.R.layout.simple_spinner_dropdown_item);

I've worked out that I can alter the colors for the drop-down Navigation in code, by creating my own subclass of ArrayAdapter as follows:

public class myArrayAdaptor<T> extends ArrayAdapter<T> {

    public myArrayAdaptor(Context context, int textViewResourceId, T[] objects) {
        super(context, textViewResourceId, objects);
    }

    public static myArrayAdaptor<CharSequence> createFromResource(Context context, int textArrayResId, int textViewResId) {
        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new myArrayAdaptor<CharSequence>(context, textViewResId, strings);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return SetColourWhite(super.getView(position, convertView, parent));
    }

    private View SetColourWhite(View v) {
        if (v instanceof TextView) ((TextView)v).setTextColor(Color.rgb(0xff, 0xff, 0xff)); // white
        return v;
    }
}

Then with the simple styles.xml

<resources>
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    </style>
</resources>

I can substitute the class myArrayAdaptor

SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list, android.R.layout.simple_spinner_dropdown_item)

and the text color will always be white. But isn't there a simpler way of doing this using settings in the styles.xml file?

Hasanaga

Java code

SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
        R.array.actions,R.layout.dropdown_textcolor);

// OnNavigationListener
OnNavigationListener callback = new OnNavigationListener() {

    String[] items = getResources().getStringArray(R.array.actions); 
    public boolean onNavigationItemSelected(int position, long id) {

        // Do stuff when navigation item is selected
        Log.d("NavigationItemSelected", items[position]); // Debug
        return true;
    }

};


ActionBar actions = getSupportActionBar();
actions.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actions.setDisplayShowTitleEnabled(false);

actions.setListNavigationCallbacks(adapter, callback);

Xml dropdown_textcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:ellipsize="marquee"
    android:textAlignment="inherit"/>

If you are using AppCompat support library .. this might help you..

<style name="MyActionBar3" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/ActionBarBackground</item>         
</style>

<style name="MyActionBarDropDownStyle" parent="">
    <item name="android:background">#00FF00</item>        
    <item name="android:divider">#ff0000</item>
    <item name="android:dividerHeight">1dp</item> 
    <item name="android:textColor">#FFFFFF</item>     
    <item name="android:textSize">10sp</item>
</style>

<style name="MyTextStyle" parent="@style/Widget.AppCompat.Light.Base.ListPopupWindow">
    <item name="android:popupBackground">#FF0000</item>        
</style>

Add this in your styles.xml that will change the color of your hidden action menu text:

<style name="AppTheme.AppCompat.Light" parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
        <item name="android:itemTextAppearance">@style/MenuTextAppearance</item>    
</style>

<style name="MenuTextAppearance">
        <item name="android:textColor">@android:color/black</item>
</style>

<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>
        <!-- Customize your theme here. -->
        <item name="android:textColor">#ffffff</item>

        <item name="android:itemBackground">#991005</item>

        <item name="android:actionBarSize">40dp</item>

        <item name="android:divider">#ff0000</item>
        <item name="android:dividerHeight">1dp</item>

        <item name="android:textSize">14sp</item>



    </style>
</resources>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!