如何在Actionbar上添加下拉菜单,而不会占用预占的空间

时间:2013-08-26 09:38:51

标签: android drop-down-menu android-actionbar

我想在Android操作栏上添加一个没有预选选项的下拉菜单。更确切地说,我希望箭头位于操作栏上应用程序的图标旁边。到目前为止,代码是:

protected void onCreate(Bundle savedInstanceState) {
    .
    .
    .
ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
             SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.logo_options,
                        android.R.layout.simple_spinner_dropdown_item);
            actionBar.setListNavigationCallbacks(spinnerAdapter, null);

    }

如何摆脱操作栏上的文字?

1 个答案:

答案 0 :(得分:5)

您可以创建一个弹出菜单,只要您单击操作栏按钮,该菜单就会下拉:

 View menuItemView = findViewById(R.id.apps);
                PopupMenu popupMenu = new PopupMenu(this, menuItemView);
                popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.windowitem1)
                            //Do whatever you want to
                        if (item.getItemId() == R.id.windowitem2)
                            //Do whatever you want to
                        if (item.getItemId() == R.id.windowitem3)
                            //Do whatever you want to

                        return false;
                    }
                });
                popupMenu.show();

ActionBar文本可以通过android:showAsAction =“”属性删除,你可以相应地设置.....