在溢出菜单中显示图标

Showing an Icon in the Overflow Menu

我正在尝试在我的一个应用程序中显示带有图标的操作溢出菜单。我没有在菜单中获得图标。我的 目标 SDK = 23最小 SDK = 16.

我的Menu.xml如下

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Single menu item
     Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_donate"
      android:icon="@drawable/new_facebook_page"
      android:title="@string/facebook"
      app:showAsAction="never" />
<item android:id="@+id/menu_settings"
      android:icon="@drawable/new_facebook_page" 
      android:title="@string/facebook"
      app:showAsAction="never" />
<item android:id="@+id/menu_logout"
      android:icon="@drawable/new_facebook_page"
      android:title="@string/facebook" 
      app:showAsAction="never" />

</menu>

Java代码如下

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);

    }

但是它没有在菜单中显示图标。让我知道是否有人可以帮助我在不更改菜单结构的情况下实现这一目标。

谢谢

onCreateOptionsMenu(Menu menu) 内的 java class 中添加以下行:

 menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));

尝试使用以下代码:

 @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_patient_home_screen, menu);


        menu.add(0, 1, 1, menuIconWithText(getResources().getDrawable(R.mipmap.user_2), getResources().getString(R.string.action_profile)));
        menu.add(0, 2, 2, menuIconWithText(getResources().getDrawable(R.mipmap.add_user), getResources().getString(R.string.action_add_user)));
        menu.add(0, 3, 3, menuIconWithText(getResources().getDrawable(R.mipmap.switch_profile), getResources().getString(R.string.action_switch_profile)));
        menu.add(0, 4, 4, menuIconWithText(getResources().getDrawable(R.mipmap.logout), getResources().getString(R.string.action_sign_out)));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(PatientHomeScreen.this, "Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 2:
                Toast.makeText(PatientHomeScreen.this, "Add New User is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 3:
                Toast.makeText(PatientHomeScreen.this, "Switch Profile is Clicked", Toast.LENGTH_SHORT).show();
                return true;
            case 4:
                Toast.makeText(PatientHomeScreen.this, "Sign Out is Clicked", Toast.LENGTH_SHORT).show();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private CharSequence menuIconWithText(Drawable r, String title) {

        r.setBounds(0, 0, r.getIntrinsicWidth(), r.getIntrinsicHeight());
        SpannableString sb = new SpannableString("    " + title);
        ImageSpan imageSpan = new ImageSpan(r, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return sb;
    }

或者,您可以通过以下方式显示带有菜单项的图标:

xml代码:

<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">
<item
    android:id="@+id/action_alarm"
    android:icon="@drawable/ic_accept"
    android:orderInCategory="100"
    android:title="@string/menu_create_alarm"
    android:showAsAction="always|withText"
    app:showAsAction="always|withText" />

Java代码:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.menu_item, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

您可以为子菜单项使用自定义布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Single menu item
     Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_donate"
      android:title="@string/facebook"
      android:actionLayout="@layout/menu_donate_layout"
      app:showAsAction="never" />
<item android:id="@+id/menu_settings"
      android:title="@string/facebook"
    android:actionLayout="@layout/menu_setting_layout"
      app:showAsAction="never" />
<item android:id="@+id/menu_logout"
      android:title="@string/facebook" 
    android:actionLayout="@layout/menu_logut_layout"
      app:showAsAction="never" />

</menu>

创建menu_donate_layout

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/profile_icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Profile" />

</LinearLayout>

同样创建每个菜单项的单独布局或动态更改文本和图标。