简体   繁体   English

工具栏菜单不再起作用

[英]Toolbar menu does not work anymore

I changed from a normal ActionBar to the new Toolbar. 我从普通的ActionBar更改为新的Toolbar。 My problem is that my old menu doesn't work. 我的问题是我的旧菜单不起作用。 When I click on the overflow icon nothing happens. 当我点击溢出图标时没有任何反应。 I'm using appcompat v7 but I could not find any solution. 我正在使用appcompat v7,但我找不到任何解决方案。 Can someone help me? 有人能帮我吗?

Menufunctions: Menufunctions:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu
    getMenuInflater().inflate(R.menu.chatmenu, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.onlineliste:
        getOnlineList();
        return true;
    case R.id.settings:
        showSettings();
        return true;
    case R.id.logout:
        logout();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

Chatmenu: Chatmenu:

        <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto" >
<item 
    android:id="@+id/onlineliste" 
    android:title="@string/userliste"
    app:showAsAction="always">
</item>
<item 
          android:id="@+id/settings" 
          android:title="@string/settings" 
          app:showAsAction="never">
</item>
    <item 
          android:id="@+id/logout" 
          android:title="@string/logout" 
          app:showAsAction="never">
</item>
</menu>

Theme 主题

<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">#428bca</item>
    <item name="colorPrimaryDark">#428bca</item>
    <item name="android:windowNoTitle">true</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark</item>
    <item name="windowActionBar">false</item>
</style>

Toolbar 工具栏

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimaryDark" />

Oh my godness I got it. 哦,天哪,我明白了。 Thank you for all your help guys! 谢谢你所有的帮助! The problem was that I had a RelativeLayout which was overlapping the toolbar but I did not see this directly. 问题是我有一个RelativeLayout与工具栏重叠,但是我没有直接看到它。 THe solution was to add 解决方案是添加

android:layout_below="@+id/toolbar" to the relativ layout and now everything works fine. android:layout_below="@+id/toolbar"更改为相对布局,现在一切正常。

Again thanks for all your help guys and sorry! 再次感谢您的帮助,对不起!

try this one: 试试这个:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto" >
<item 
    android:id="@+id/onlineliste" 
    android:visible="true" 
    android:title="@string/userliste" 
    app:showAsAction="never">
</item>
<item 
          android:id="@+id/settings" 
          android:visible="true" 
          android:title="@string/settings" 
          app:showAsAction="never">
</item>
    <item 
          android:id="@+id/logout" 
          android:visible="true" 
          android:title="@string/logout" 
          app:showAsAction="never">
</item>

also your theme must have a parent of Theme.AppCompat and your Activity must extends from ActionBarActivity 您的主题也必须具有Theme.AppCompat的父级,并且您的Activity必须从ActionBarActivity扩展

Probably you need to set your Toolbar as the activity ActionBar, using: 您可能需要将Toolbar设置为活动ActionBar,使用:

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.your_toolbar);
    setSupportActionBar(toolbar);

You are not returning true from the onCreateOptionMenu(). 你没有从onCreateOptionMenu()返回true。

The documentation says that 该文件说

You must return true for the menu to be displayed; if you return false it will not be shown.

Insead of 侵害

return super.onCreateOptionsMenu(menu);

which always return false. 总是返回虚假。 Return true from onCreateOptionsMenu(menu); 从onCreateOptionsMenu(菜单)返回true;

Check out the documentation here. 在这里查看文档。 Source Link 来源链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM