即使使用app:showAsAction,Android菜单项也会显示在溢出区域中

问题描述:

我正在使用MainActivity来扩展Activity类。项目使用的最小API为11.当我充气菜单项时,它总是显示为溢出。即使使用app:showAsAction,Android菜单项也会显示在溢出区域中

public class MainActivity extends Activity { 
.... 
@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    // Inflate the Menu Items 
    getMenuInflater().inflate(R.menu.menu_main,menu); 
    return true; 
} 

menu_main.xml

<?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" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context=".MainActivity"> 
<item android:id="@+id/attach" 
    android:title="Attachment" 
    android:orderInCategory="1" 
    app:showAsAction="always"/> 

</menu> 

Styles.xml

<!-- Base application theme. --> 
<style name="AppTheme" parent="android:Theme.Holo.Light"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

+1

请不要把代码 – Raghavendra

+0

后的截图menu_main.xml文件 –

你MainActivity扩展活动不AppcompatActivity和您的Android:Theme.Holo.Light本土题材的话,可你刚刚更换

应用:showAsAction = “总是” 与Android:showAsAction = “总是”

和尝试,像

<?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" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context=".MainActivity"> 
<item android:id="@+id/attach" 
    android:title="Attachment" 
    android:orderInCategory="1" 
    android:showAsAction="always"/> 

</menu> 
+0

谢谢。它引用了旧的AppcompatActivity依赖关系。 –

+0

当我扩展Activity并使用android:showAsAction =“always”时,这个答案会起作用。这个答案是错误的@ Biswajitghosh –

+0

@AmitVaghela你正在使用哪个主题? – Raghavendra

设置为showAsAction = “总是”

+0

仍然没有帮助.. –

试试这个代码,

溢出显示:

yourapp:showAsAction="always" 

显示溢

yourapp:showAsAction="never" 

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourapp="http://schemas.android.com/apk/res-auto"> 
    <!-- Search, should appear as action button --> 
    <item 
     android:id="@+id/attach" 
     android:icon="@drawable/ic_action_hardware_keyboard_arrow_left" 
     android:title="Attachment" 
     android:orderInCategory="1" 
     yourapp:showAsAction="ifRoom" /> 

</menu> 

检查这个menu resource doc

+0

尽我更新的答案,并告诉我,如果你有任何问题,@ Biswajit戈什 –

+0

HI阿米特感谢您的回答 。但只有当你的项目对AppCompat有依赖时它才能工作。由于我删除了Appcompat依赖项,所以我不得不使用android:showAsAction而不是像yourapp这样的自定义名称空间。 –