在android中以动态和编程方式添加菜单项

时间:2016-03-21 14:24:21

标签: android menu menuitem

@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {


    new Thread(new Runnable() {

        @Override
        public void run() {
            AdminSoapService adminSoapService = new AdminSoapService();
            final List<String> packageNamesList = adminSoapService.getPackagesFromSumarClientByClientCnp("1840805160041");
            String[] packageNames = packageNamesList.toArray(new String[packageNamesList.size()]);
            packagesViews = SQLMceHelper.getDb(getActivity()).getPackagesByPackageNames(packageNames);

            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < packagesViews.size(); i++) {

                        String name = packagesViews.get(i).getName();
                        String path = packagesViews.get(i).getTileRenderPath();
                        int id = packagesViews.get(i).getId();                          

                        MenuItem edit_item = menu.add(Menu.NONE, id , i, name);
                        edit_item.setIcon(Drawable.createFromPath(path));
                        edit_item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                        edit_item.setOnMenuItemClickListener(SCFragment.this);

                    }                       
                }
            });


        }
    }).start();

    super.onCreateOptionsMenu(menu, inflater);
}

以上代码的问题如下: 1:如果是房间,则仅显示图像 2:如果不是房间,则文本显示在二级菜单中

我需要显示图像和文本,如果是空间,如果不是,我也希望文本在图像下方。

1 个答案:

答案 0 :(得分:1)

@Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {

//      for (int i = 0; i < 3; i++) {
//          MenuItem add = menu.add(0,  i, i, "Menu" + i);
//          add.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
//          add.setActionView(R.layout.action_button_menu_item);
//          ((TextView)add.getActionView().findViewById(R.id.textViewFromMenuItem)).setText("Menu " + i);
//          ((ImageView)add.getActionView().findViewById(R.id.imageViewFromMenuItem)).setBackground(Drawable.createFromPath(pathName));;
//      }

        new Thread(new Runnable() {

            @Override
            public void run() {
                AdminSoapService adminSoapService = new AdminSoapService();
                final List<String> packageNamesList = adminSoapService.getPackagesFromSumarClientByClientCnp("1840805160041");
                String[] packageNames = packageNamesList.toArray(new String[packageNamesList.size()]);
                packagesViews = SQLMceHelper.getDb(getActivity()).getPackagesByPackageNames(packageNames);

                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        for (int i = 0; i < packagesViews.size(); i++) {

                            String name = packagesViews.get(i).getName();
                            System.out.println("name " + name);
                            String path = packagesViews.get(i).getTileRenderPath();
                            int id = packagesViews.get(i).getId();


                            MenuItem add = menu.add(0,  id, i, name);
                            add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
                            add.setActionView(R.layout.action_button_menu_item);
                            ((TextView)add.getActionView().findViewById(R.id.textViewFromMenuItem)).setText(name);
                            ((ImageView)add.getActionView().findViewById(R.id.imageViewFromMenuItem)).setBackground(Drawable.createFromPath(path));                     
                        }                       
                    }
                });

            }
        }).start();

        super.onCreateOptionsMenu(menu, inflater);
    }