2016-07-24 11 views
1

Ich studiere Android jetzt und ich blieb mit einem Problem stecken, das ich lösen konnte. Das Menü erscheint nicht in der Spitze meiner einfachen Anwendung:Menü erscheint nicht in der Hauptaktivität

<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-Code:

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); 
    } 
} 

Antwort

1

Sie versteckt sind, weil Sie app:showAsAction="never" im Menü XML-Datei festgelegt. den Wert Aktualisieren mit always statt never

+0

Dank jetzt teste ich die Anwendung auf Jelly Bean 4.3 noch ist es nicht als Drop-Down-Liste zeigt es die Elemente auf einer Zeile im Menü warum eine Idee zeigt es zeigt wie dies –

+0

Wenn Sie ein Symbol in der Aktionsleiste und ein Dropdown für sie mit den Rest der Elemente haben möchten, müssen Sie eine Struktur wie folgt haben: ' ...' und in das Menü haben die 3 Gegenstände, die du jetzt benutzt. Sehen Sie sich dieses Beispiel an, das ich gemacht habe: http://pastebin.com/KtdsPxB2 – Ibrahim

Verwandte Themen