简体   繁体   English

Android 工具栏图标不显示

[英]Android toolbar icon doesn't show

I'm struggling with 2 menu items that should display on a toolbar in their icon fashion, but they always display on the toolbar in a drop-down menu fashion.我正在努力处理 2 个应该以图标方式显示在工具栏上的菜单项,但它们总是以下拉菜单方式显示在工具栏上。

Here is my code so far,到目前为止,这是我的代码,

MainActivity.java主活动.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 :活动_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工具栏_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 : -here is the part that should make the difference 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>

Any help is welcomed.欢迎任何帮助。

You are going good but just one line correction change this line in menu.xml你一切顺利,但只有一行更正更改了menu.xml 中的这一行

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

Remove the 's' from httphttp 中删除“s”

You can menu_main.xml like ...bellow你可以 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>

Make Change Like this.像这样做出改变。 It will show both items.它将显示这两个项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM