菜单没有出现在主要活动中

问题描述:

我正在学习Android,并且遇到了一个我可以解决的问题。 菜单没有出现在我的简单应用程序的顶部:菜单没有出现在主要活动中

<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"> 

<group android:checkableBehavior="single"> 

    <item android:id="@+id/red_button" 
     android:title="@string/red" 
     android:checkable="true" 
     android:orderInCategory="1" 
     app:showAsAction="never" /> 
    <item android:id="@+id/green_button" 
     android:title="@string/green" 
     android:checkable="true" 
     android:orderInCategory="1" 
     app:showAsAction="never"/> 
    <item android:id="@+id/yellow_button" 
     android:title="@string/yellow" 
     android:checkable="true" 
     android:orderInCategory="1" 
     app:showAsAction="never"/> 
</group> 

</menu> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/layout" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.emad.menuapp.MainActivity"> 


</RelativeLayout> 

Java代码:

public class MainActivity extends AppCompatActivity { 

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


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.main_menu,menu); 

     return true; 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     RelativeLayout lay=(RelativeLayout) findViewById(R.id.layout); 


     switch (item.getItemId()){ 
      case(R.id.red_button): 
       if (item.isChecked()) 
        item.setChecked(false); 
       else item.setChecked(true); 

       lay.setBackgroundColor(Color.RED); 

       return true; 

      case(R.id.green_button): 
       if (item.isChecked()) 
        item.setChecked(false); 
       else item.setChecked(true); 
       lay.setBackgroundColor(Color.GREEN); 

       return true; 

      case(R.id.yellow_button): 
       if (item.isChecked()) 
        item.setChecked(false); 
       else item.setChecked(true); 
       lay.setBackgroundColor(Color.YELLOW); 

       return true; 

     } 


     return super.onOptionsItemSelected(item); 
    } 
} 

他们被隐藏,因为你的菜单XML文件中设置app:showAsAction="never"。 更新值always而不是never

+0

谢谢我现在正在测试应用程序的果冻豆4.3仍然不显示为下拉列表它显示菜单中的一行上的项目任何想法为什么它显示像这 –

+0

如果你想在操作栏中有一个图标,并为它与其余项目的下拉,你将需要一个像这样的结构:'

....'并在菜单中有您正在使用的3个项目。看看这个例子:http://pastebin.com/KtdsPxB2 – Ibrahim