2017-07-26 9 views
0

Ich habe Probleme mit 2 Menüpunkte, die auf einer Symbolleiste in ihrer Symbol-Mode angezeigt werden sollte, aber sie immer auf der Symbolleiste in einem Drop-Down-Menü Mode angezeigt.Android Toolbar-Symbol zeigt nicht

meinen Code Hier ist so weit,

MainActivity.java

package com.yardimobileinterns.apptoolbar; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

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

     setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    //menu icons are inflated 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     //inflate the menu. This adds items to the action bar, if it is present 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem menuItem) { 
     int id = menuItem.getItemId(); 

     switch (id) { 
      case R.id.miCompose: 
       composeMessage(); 
       break; 
      case R.id.miProfile: 
       showProfileView(); 
       break; 
     } 
     return super.onOptionsItemSelected(menuItem); 
    } 


    private void composeMessage() { 
     getSupportActionBar().setTitle("Compose"); 
    } 

    private void showProfileView() { 
     getSupportActionBar().setTitle("Profile"); 
    } 

} 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/toolbar_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="100dp" 
     /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/button" 
     android:layout_gravity="center" 
     android:layout_marginTop="100dp" 
     /> 

</LinearLayout> 

toolbar_main.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorPrimary" 
    android:minHeight="?attr/actionBarSize" 
    app:titleTextColor="@android:color/white" 
    /> 

menu_main.xml: - Hier ist das Par t das sollte den Unterschied machen

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="https://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 

    <item 
     android:id="@+id/miCompose" 
     android:icon="@drawable/menu" 
     android:title="@string/item1" 
     app:showAsAction="always"> 
    </item> 

    <item 
     android:id="@+id/miProfile" 
     android:icon="@drawable/week_view" 
     android:title="@string/profile" 
     app:showAsAction="always"> 
    </item> 

</menu> 

Jede Hilfe ist willkommen.

+0

möchten Sie i Nachteile in der Symbolleiste oder Menü –

+0

Hinzufügen Android: orderInCategory = "1" Attribut für jedes Element. Und ändern Sie die Kategorienummer nach Ihrer Anforderung –

Antwort

2

Sie werden gut, aber nur eine Zeile Korrektur Änderung dieser Linie in menu.xml

xmlns:app="http://schemas.android.com/apk/res-auto" 

das 's' entfernen http

+0

Es funktioniert! Danke vielmals! –

+1

willkommen und glücklich, Ihnen zu helfen! –

+0

Es ist hilfreich, als meine Antwort auf andere Entwickler Hilfe –

0

Ändern Sie so. Ich werde beide Gegenstände zeigen.

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="https://schemas.android.com/apk/res-auto" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 

    <item 
     android:id="@+id/miCompose" 
     android:icon="@drawable/menu" 
     android:title="@string/item1" 
     android:orderInCategory="1" 
     app:showAsAction="always"> 
    </item> 

    <item 
     android:id="@+id/miProfile" 
     android:icon="@drawable/week_view" 
     android:title="@string/profile" 
     android:orderInCategory="2" 
     app:showAsAction="always"> 
    </item> 

</menu> 
1

Sie können menu_main.xml wie ... bruder

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:balloonberry="http://schemas.android.com/apk/res-auto"> 

<item 
    android:id="@+id/miCompose" 
    balloonberry:showAsAction="always" 
    android:icon="@drawable/menu"/> 
<item 
    android:id="@+id/miProfile" 
    balloonberry:showAsAction="always" 
    android:icon="@drawable/week_view"/> 

    </menu>