Android工具栏图标不显示

时间:2017-07-26 11:00:19

标签: android android-xml android-toolbar android-icons

我正在苦苦寻找应以图标方式显示在工具栏上的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>

欢迎任何帮助。

3 个答案:

答案 0 :(得分:2)

你很顺利,但只有一行修正在 menu.xml

中改变了这一行
<option [value]="i" *ngFor="let i of vatCodeList">{{ i }}</option>

删除&#39;&#39;来自 http

答案 1 :(得分:1)

你可以像menu_main.xml一样... bellow

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

答案 2 :(得分:0)

像这样改变。我会展示这两个项目。

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