操作栏样式的溢出菜单项

4

我需要制作完全定制的溢出菜单项(至少与图片上显示的背景颜色不同)。

https://dl.dropbox.com/u/7771649/overflow.png

这可能吗?


可以改变所有溢出菜单项的颜色,但我不认为可以单独设置它们。参考链接 - Cat
但是我需要每个项目的不同样式。我知道一种方法 - 使用自己的布局PopupWindow。但它不像操作栏中的弹出菜单那样优雅。 - user2069363
您可以通过设置操作列表的适配器来实现这一点。在适配器中,您可以设置不同的背景颜色。 - Mohammad Imran
@MohammadImran 请给我更多的细节! - user2069363
我已经回答了你的问题。如果你需要更多帮助,我会提供给你。 - Mohammad Imran
@user2069363,如果你得到了答案,请回复我的回答。 - Mohammad Imran
3个回答

5

昨天我需要实现ActionBarSherlock,并且我的情况与你的相同。我必须为我的导航列表实现适配器。例如:

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);

欲了解更多信息,请查看此处


好的解决方案!但是我更喜欢这个:https://dev59.com/AWzXa4cB1Zd3GeqPWrZS#14123377 因为它允许我在与原始溢出菜单相同的位置创建带有溢出菜单图标的Spinner。 - user2069363
是的,你可以添加图标,如果你想的话。如果需要,我可以更新我的答案以包含图标。 - Mohammad Imran
同样的解决方案,基本主题是相同的。接受我的答案。 - Mohammad Imran

0

2
当 MenuItem 显示在操作栏中时,它可以工作。但是当菜单项位于溢出菜单中时,它无法工作。 - user2069363

0

这个 https://dev59.com/AWzXa4cB1Zd3GeqPWrZS#14123377 对我很有效!

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接