2017-09-06 84 views
1

我正在使用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文件 –

回答

2

你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

0

設置爲showAsAction = 「總是」

+0

仍然沒有幫助.. –

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這樣的自定義名稱空間。 –

相關問題