How can I override my menu bar to come on my application bar?

痞子三分冷 提交于 2019-12-25 08:09:03

问题


I have recycler view + viewpager + toolbar in my activity. When I long click on the row in recycler view it should show me a menu bar with a delete option. But this is what I get. Any ideas as to why it is coming above my application name?

This is the xml of my main activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kaad.sampleproject.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_activity_tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/main_activity_tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/main_activity_tool_bar"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:tabIndicatorColor="#ff00ff"
        app:tabMode="fixed"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextColor="#d3d3d3" />

    <android.support.v4.view.ViewPager
        android:id="@+id/main_activity_viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/main_activity_tablayout" />

</RelativeLayout>

I kept an add and about menu which never gets displayed for some reason: Here is the xml of both my menus:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/menu_bar_main"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:context=".MainActivity">
    <item
        android:id="@+id/menu_item_new"
        android:icon="@drawable/ic_add_white_36dp"
        android:title="@string/menu_new"
        app:showAsAction="ifRoom|withText" />
    <item
        android:id="@+id/menu_item_about"
        android:title="@string/menu_about"
        app:showAsAction="never" />
</menu>

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/menu_bar_delete"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    tools:context=".MainActivity">
    <item android:id="@+id/menu_item_delete"
        android:icon="@android:drawable/ic_menu_delete"
        android:title="@string/delete" />
</menu>

The xml of my styles

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <!--<item name="colorPrimary">#FF595951</item>-->
        <!--<item name="colorPrimaryDark">#FF5C5C5C</item>-->
        <!--<item name="colorAccent">#FFCCCCCC</item>-->
        <!--<item name="android:textColorPrimary">#FF0FF0</item>-->
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
    </style>

This is Main Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_main);

        database = new Database(this);
        categories = database.getCategories();

        myToolbar = (Toolbar) findViewById(R.id.main_activity_tool_bar);
        setSupportActionBar(myToolbar);


        // Get the ViewPager and set it's PagerAdapter so that it can display items
        myViewPager = (ViewPager) findViewById(R.id.main_activity_viewpager);
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), MyMainActivity.this, categories);
        myViewPager.setAdapter(myPagerAdapter);

        // Give the TabLayout the ViewPager
        myTabLayout = (TabLayout) findViewById(R.id.main_activity_tablayout);
        myTabLayout.setupWithViewPager(myViewPager);

        // Iterate over all tabs and set the custom view
        for (int i = 0; i < myTabLayout.getTabCount(); i++) {
            TabLayout.Tab myTab = myTabLayout.getTabAt(i);
            myTab.setCustomView(myPagerAdapter.getTabView(i));
        }
        bus.getInstance().register(this);
    }

This is how I am calling my menu bar in my main prescription fragment

 public class CustomMultiSelectorCallback extends ModalMultiSelectorCallback {

        private int count;
        public CustomMultiSelectorCallback(MultiSelector multiSelector) {
            super(multiSelector);
        }

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            super.onCreateActionMode(actionMode, menu);
            count = 0;
            actionMode.getMenuInflater().inflate(R.menu.list_item_delete, menu);
            return true;
        }

回答1:


Issue for About button:

<item
    android:id="@+id/menu_item_about"
    android:title="@string/menu_about"
    app:showAsAction="never"  />

update the never part to "ifRoom"

Regarding the menu appearing, can you be more explicit, as to me it seems in Relative Layout you wanted the Toolbar to be top and the views to come after it and that is what it shows.




回答2:


So I have solved one of the problems. This is what I edited in my code to display add and about buttons in the toolbar.

First, in your onCreate in the fragments, add this line:

setHasOptionsMenu(true);

Second, in your styles, change your theme to something which has NoActionBar. In my case I did this:

<style name="AppThemeOfStyles"parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#FF595951</item>
        <item name="colorPrimaryDark">#FF5C5C5C</item>
        <item name="colorAccent">#FFCCCCCC</item>
        <item name="android:textColorPrimary">#FF0FF0</item>
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">true</item>
    </style>

I also changed the name of my theme from AppTheme to AppTheme of styles since AppTheme is already a theme defined. So better to change your theme name to something else.

This leads us to the result below:

But my delete is still coming on the top of the toolbar on longpress.

Any suggestions, advice will be appreciated :)

UPDATED: Use this in your theme for the overlay to work: <item name="windowActionModeOverlay">true</item>

Here is the end result:



来源:https://stackoverflow.com/questions/38982673/how-can-i-override-my-menu-bar-to-come-on-my-application-bar

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