【问题标题】:Implementing three actions inside a single menu item in Android在 Android 的单个菜单项中实现三个操作
【发布时间】:2012-12-29 15:49:24
【问题描述】:

我希望在移动版 chrome 浏览器中拥有与图片中类似的菜单项功能。我想在一行中有后退、前进、刷新菜单项。如何实现类似的菜单项?是否有任何参考或是否有任何技巧可以带来此功能?

我的应用仅针对平板电脑。这是我当前的操作栏菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/favorite"
    android:showAsAction="always"
    android:title="Happy"/>
<item
    android:id="@+id/share_button"
    android:icon="@drawable/share"
    android:showAsAction="always"
    android:title="Share"/>
<item
    android:id="@+id/hola_button"          
    android:showAsAction="always"
    android:title="Hola"/>
<item
    android:icon="@drawable/more_actions"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/back_button"
            android:icon="@drawable/back"
            android:title="back"/>
        <item
            android:id="@+id/forward_button"
            android:icon="@drawable/forward"
            android:title="forward"/>
        <item
            android:id="@+id/refresh_button"
            android:icon="@drawable/refresh"
            android:title="refresh"/>          
    </menu>
 </item>
 </menu>

【问题讨论】:

    标签: android menu android-actionbar android-menu android-browser


    【解决方案1】:

    编辑:

    这个例子是一个溢出菜单(因为 ABS 放弃了溢出主题)。你可以膨胀任何类型的布局组合。此类从 PopUpWindow 扩展而来,不使用典型的 onCreateOptions。它使用 ABS-CustomView 和 PopUpWindow 来创建菜单。

    来自 android 参考:可用于显示任意视图的弹出窗口。弹出窗口是一个浮动容器,出现在当前活动的顶部。

    布局看起来与您请求的布局相似。此视图锚定到 ActionBar,但您可以在任何您想要的地方显示它。弹出窗口支持许多开箱即用的显示功能。

    可自定义的溢出菜单

    public class OverflowMenu extends PopupWindow {
    
    private View mContentView;
    
    public OverflowMenu(Context context, int resourceId) {
        super(context);
        mContentView = LayoutInflater.from(context).inflate(resourceId, null);
        setContentView(mContentView);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
    
        // Handle touchevents
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new BitmapDrawable());
    }
    
    /**
     * Attach the OverflowMenu View to the ActionBar's Right corner
     * @param actionBarView
     */
    public void show(View actionBarView) {
        int x = actionBarView.getMeasuredWidth() - mContentView.getMeasuredWidth();
        showAsDropDown(actionBarView, x, 0);
    }
    
    /**
     * Return mContentView, 
     *  used for mContentView.findViewById();
     * @return
     */
    public View getView(){
        return mContentView;
    }
    }
    

    MainActivity

    public class MainActivity extends SherlockActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mai n);
    
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.actionbar);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    
        final OverflowMenu menu = new OverflowMenu(this,R.layout.menu_overflow);
    
        final ImageButton overflowBtn = (ImageButton) actionBar.getCustomView().findViewById(R.id.actionbar_overflow);
        overflowBtn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                menu.show(actionBar.getCustomView());
            }
        });
      }
    
    
     }
    

    【讨论】:

    • 我有 6 个菜单项。其中 3 个将在操作栏中,其中 3 个应在更多操作隐藏菜单下。我仅在更多操作菜单上需要此功能。我会尝试这个弹出窗口并回来。
    • 这是一个有效的用例,我建议在 ABS 上实现一个 customView,并添加 4 个图标(第四个是一个过低的图标)。按下第四个时,您可以显示弹出窗口或弹出菜单窗口。
    【解决方案2】:

    这看起来像一个带有列表视图和自定义列表头的自定义对话框

    一个简单布局下方的列表视图,对话框顶部有 3 个按钮。

    您可以在操作栏菜单项单击上显示相同的内容。

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.menu_items, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case android.R.id.show_dlg:
    
        // Show your custom dialog
            break;
        }
    
        return super.onOptionsItemSelected(item);
    
    }
    

    此外,本教程将有助于作为膨胀自定义菜单的参考 http://www.codeproject.com/Articles/173121/Android-Menus-My-Way

    【讨论】:

      【解决方案3】:

      您可以使用以下方式将菜单项添加到页面:

      OptionCommand command = new OptionCommand();
      command.setActionView(view);
      command.setIcon(canvas.getContext().getResources().getDrawable(android.R.drawable.ic_menu_search));
      command.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
      canvas.getActivity().getOptionCommands().add(command);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多