【问题标题】:Actionbar styled overflow menu items操作栏样式的溢出菜单项
【发布时间】:2013-02-14 02:51:05
【问题描述】:

我需要制作完全自定义的溢出菜单项(不同的背景颜色,如图所示)。

有可能吗?

【问题讨论】:

  • possible to change the color of all overflow menu items,但我不相信可以单独设置它们。
  • 但我需要为每个项目提供不同的样式。我知道一种方法 - 具有自己布局的 PopupWindow。但它不像操作栏中的弹出菜单那么优雅。
  • 你可以通过设置你的动作列表的适配器来实现这个设置。在适配器中你可以设置不同的背景颜色。
  • @MohammadImran 请给我更多细节!
  • 我已经回答了您的问题。如果您需要更多帮助,我会为您提供。

标签: android themes android-actionbar


【解决方案1】:

最后一天我必须实施 Action Bar Sherlock。我的情况和你的一样。我已经为我的导航列表实现了适配器。假设:

public class MyNavigationAdapter extends BaseAdapter {
    Context mContext = null;
    String[] mTitles;
    LayoutInflater mLayOutInflater = null;

    public MyNavigationAdapter(Context context, String[] titles) {
        this.mContext = context;
        this.mTitles = titles;
        mLayOutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null)
            view = mLayOutInflater.inflate(R.layout.my_list_custom_row, parent,false);
        TextView rowName = (TextView) view.findViewById(R.id.title);
        rowName.setText(mTitles[position]);
        rowName.setBackground(your desire drawle);// yo

u can also set color etc.
//OR

if(position%2==0)
        rowName.setBackgroundColor(Color.RED);
        else
            rowName.setBackgroundColor(Color.BLUE);
        return view;
    }

}

在这之后,你必须在你的Activity onCreate方法中编写这行代码。

  ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(mNavigationAdapterObj, mNavigationListner);

更多信息请咨询here.

【讨论】:

  • 很好的解决方案!但我更喜欢这个:stackoverflow.com/a/14123377/2069363 因为它允许我在与原始溢出菜单一样的位置创建带有溢出菜单图标的微调器。
  • 是的,如果需要,您也可以添加图标。如果你愿意,我可以更新我的图标答案。
  • 这是相同的解决方案,基本主题相同。接受我的回答。
【解决方案2】:

您可以为每个 MenuItem 设置自定义视图。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("test").setActionView(R.layout.custom_action_menu_item);
    return super.onCreateOptionsMenu(menu);
}

http://developer.android.com/reference/android/view/MenuItem.html#setActionView%28int%29

【讨论】:

  • 当 MenuItem 显示在操作栏中时有效。但是当菜单项在溢出菜单中时不起作用。
【解决方案3】:

这个https://stackoverflow.com/a/14123377/2069363 为我工作!

您可以在菜单项的操作布局中使用 Spinner(或用于 ActionBarSherlock 的 IcsSpinner)创建此类行为。虽然您必须使用一个小技巧 - 隐藏当前选定的项目。

【讨论】: