简体   繁体   English

Android ActionBar菜单始终显示溢出(没有AppCompat)

[英]Android ActionBar Menu always showing overflow (Without AppCompat)

在此处输入图片说明

No matter the screen size, the menu is always showing 3 dots instead of just showing a single icon on the right corner. 无论屏幕大小如何,菜单始终显示3个点,而不是在右上角仅显示一个图标。

I have seen similar questions, but none of them without AppCompat. 我见过类似的问题,但没有AppCompat都没有。 I am using minSdkVersion 14 on my build.gradle file, and the following dependencies: 我在build.gradle文件和以下依赖项上使用minSdkVersion 14:

compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v13:23.1.1'

Principal.Java relevant code: Principal.Java相关代码:

import android.app.ActionBar;

public class Principal extends Activity {
  protected void onCreate(Bundle savedInstanceState) {
    ActionBar actionBar = getActionBar()
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.options_menu, menu);
      return true;
  }
}

Here is my options_menu.xml: 这是我的options_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_general">
     <item android:id="@+id/menu_search"
          android:title="@string/search_label"
          android:icon="@drawable/ic_action_search"/> 
</menu>

Add this in the Method Oncreate:Change what u need 在Oncreate方法中添加此内容:更改所需的内容

 Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(topToolBar);
            topToolBar.setLogo(R.drawable.logo);

You should set the following property in your menuItem: app:showAsAction="always" 您应该在menuItem中设置以下属性: app:showAsAction =“ always”

<?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"
      android:id="@+id/menu_general">
          <item android:id="@+id/menu_search"
                android:title="@string/search_label"
                app:showAsAction="always"
                android:icon="@drawable/ic_action_search"/> 
   </menu>

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

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