如何覆盖菜单栏以出现在应用程序栏上? [英] How can I override my menu bar to come on my application bar?
问题描述
我的活动中有回收站视图+ viewpager +工具栏.当我在回收站视图中长按该行时,它应该显示一个带有删除选项的菜单栏.但这就是我得到的.关于它为什么出现在我的应用程序名称上方的任何想法?
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?
这是我主要活动的xml:
<?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>
我保留了一个关于"菜单,该菜单由于某种原因从未显示过: 这是我两个菜单的xml:
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>
我的样式的xml
<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>
这是主要活动:
@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);
}
这就是我在主处方片段中调用菜单栏的方式
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;
}
推荐答案
所以我已经解决了其中一个问题.这是我在代码中编辑的内容,用于显示工具栏中的添加和关于按钮.
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.
首先,在片段的onCreate中,添加以下行:
First, in your onCreate in the fragments, add this line:
setHasOptionsMenu(true);
其次,以您的样式将主题更改为具有NoActionBar
的主题.就我而言,我是这样做的:
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>
我也将主题的名称从AppTheme更改为样式的AppTheme,因为AppTheme已经定义了主题.因此最好将您的主题名称更改为其他名称.
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:
但是我的删除仍然在longpress的工具栏顶部.
But my delete is still coming on the top of the toolbar on longpress.
任何建议,建议将不胜感激:)
Any suggestions, advice will be appreciated :)
更新:
在主题中使用此选项可使叠加层起作用:
<item name="windowActionModeOverlay">true</item>
UPDATED:
Use this in your theme for the overlay to work:
<item name="windowActionModeOverlay">true</item>
这是最终结果:
这篇关于如何覆盖菜单栏以出现在应用程序栏上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!