How to add menu button without action bar?

送分小仙女□ 提交于 2019-11-27 12:20:50

问题


I'd like to add a menu button to the right top corner of my app and without action bar, like it is in Google Fit app on the screenshot below. Can anyone help me?


回答1:


You can simply use PopupMenu, for example add the following to a button when clicked:

public void showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.actions, popup.getMenu());
    popup.show();
}

For more info, read Creating a Popup Menu : http://developer.android.com/guide/topics/ui/menus.html




回答2:


Add a toolbar the layout and make it transparent. That is the best solution to adding menu items to a layout while giving the appearance there is no actionbar/toolbar.

Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The rest of your code here -->

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@android:color/transparent"/>

</RelativeLayout>

Theme

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>
</resources>

Example of inflating the menu, setting title, menu click listener.

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Toolbar");
toolbar.inflateMenu(R.menu.menu_main);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.action_refresh) {

        }
        return false;
    }
});

Don't set the Toolbar as the action bar. The theme just removes it totally.




回答3:


<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_overflow_holo_dark"
    android:contentDescription="@string/descr_overflow_button"
    android:onClick="showPopup" />

Add above lines in xml file where you want to show this menu.

public void showMenu(View v) {

    PopupMenu popup = new PopupMenu(this, v);
    // This activity implements OnMenuItemClickListener
    popup.setOnMenuItemClickListener(this);
    popup.inflate(R.menu.actions);
    popup.show();
}

@Override
public boolean onMenuItemClick(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.archive:
            archive(item);
            return true;
        case R.id.delete:
            delete(item);
            return true;
        default:
            return false;
    }
}

For more details go through: https://developer.android.com/guide/topics/ui/menus.html




回答4:


I do not think you can add a menu without action bar. But, there are 2 approaches I can think off.

  1. Create and Action bar of the same color as that of the background so that it does not show, next add the menus.

  2. Just add a button on the top right corner of the screen and put the drop down in the layout.

Or Use a Pop up menu as suggested by @M-Y



来源:https://stackoverflow.com/questions/30417223/how-to-add-menu-button-without-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!