Android 工具栏图标不显示

Android toolbar icon doesn't show

我正在努力解决 2 个应该以图标方式显示在工具栏上的菜单项,但它们总是以下拉菜单方式显示在工具栏上。

到目前为止,这是我的代码,

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 :-这里是应该有所作为的部分

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

欢迎任何帮助。

像这样进行更改。它将显示这两个项目。

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

你做的很好,但是在 menu.xml

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

http

中删除 's'

你可以menu_main.xml喜欢...下面

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