Android 工具栏溢出菜单更改文本颜色并将菜单填充到屏幕

Android toolbar overflow menu change text color and fill menu to the screen

我在更改 toolbar/action 栏溢出菜单项的文本颜色时遇到问题。我做了太多的谷歌搜索和阅读 Whosebug 的答案,但它不能解决我的问题

当前我正在将黑色添加到溢出菜单项中。

我想要的是 1. 我想将菜单项的颜色从黑色改为白色。 2. 我想改变溢出菜单的宽度以匹配父屏幕

我的代码:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColorSecondary">@color/secondary_text</item>
    <item name="android:divider">@color/divider</item>
    <item name="icon">@color/icons</item>
    <item name="actionOverflowMenuStyle">@style/AppTheme.Base.PopupMenu</item>
</style>

<style name="AppTheme.Base.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">@color/primary</item>
</style>

请帮我解决这个问题..

提前致谢

我终于得到了将黑色转换为白色的代码

public boolean onPrepareOptionsMenu(Menu menu) {
    //return super.onPrepareOptionsMenu(menu);
    try {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem mi = menu.getItem(i);
            mi.setIcon(R.drawable.ic_action_new);
            String title = mi.getTitle().toString();
            Spannable spannable = new SpannableString(title);
            spannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            mi.setTitle(spannable);
        }
    } catch (Exception ex) {

    }
    return true;
}

一种更灵活但不太受欢迎的方法是使用 PopupWindow

MainActivity.java 包裹 com.chiragjn.stackovefflowing;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

    String TAG = "MainActivity.java";
    String popUpContents[];
    PopupWindow popupWindow;
    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        List<String> itemsList = new ArrayList<String>();
        itemsList.add("Option 1");
        itemsList.add("Option 2");
        itemsList.add("Option 3");
        itemsList.add("Option 4");
        popUpContents = new String[itemsList.size()];
        itemsList.toArray(popUpContents);
        popupWindow = popupWindow();
    }

    public PopupWindow popupWindow() {

        PopupWindow popupWindow = new PopupWindow(this);
        ListView listView = new ListView(this);
        listView.setAdapter(dogsAdapter(popUpContents));
        popupWindow.setFocusable(true);
        popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(listView);
        return popupWindow;
    }

    private ArrayAdapter<String> dogsAdapter(String dogsArray[]) {

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dogsArray) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                String item = getItem(position);
                String text = item;


                TextView listItem = new TextView(MainActivity.this);

                listItem.setText(text);
                listItem.setTextSize(22);
                listItem.setPadding(10, 10, 10, 10);
                listItem.setTextColor(Color.WHITE);
                switch(position)
                {
                    case 0:
                        listItem.setBackgroundColor(Color.BLUE);
                        break;
                    case 1:
                        listItem.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        listItem.setBackgroundColor(Color.YELLOW);
                        break;
                    case 3:
                        listItem.setBackgroundColor(Color.RED);
                        break;
                    default:
                    break;
                }
                return listItem;
            }
        };

        return adapter;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.action_options:
            View v = findViewById(R.id.action_options);
            popupWindow.showAsDropDown(v, 0, 0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.chiragjn.stackovefflowing.MainActivity" >

    <item android:id="@+id/action_options"
          android:icon="@android:drawable/ic_menu_more"
          android:title="Overflow Menu"
          app:showAsAction="always"  />
</menu>

然后您可以在 ListView 中扩充您最喜欢的布局并实现 ListView onItemClickListner()