Android 工具栏设置了错误的图标

Android 工具栏设置了错误的图标,android,android-layout,android-toolbar,Android,Android Layout,Android Toolbar,在我的应用程序中,我想为工具栏设置一个自定义菜单。我在菜单设置.xml中定义的图标是一个箭头,但当我启动应用程序时,它是一个三点菜单,而不是箭头(如图所示)。经过数小时的研究,我仍然不明白为什么它会加载这个图标而不是箭头(是的,我检查了可绘制的;)。感谢您提供的解决方案和提示 设置活动性: public class SettingsActivity extends PreferenceActivity { private AppCompatDelegate mDelegate; @Ove

在我的应用程序中,我想为工具栏设置一个自定义菜单。我在
菜单设置.xml
中定义的图标是一个箭头,但当我启动应用程序时,它是一个三点菜单,而不是箭头(如图所示)。经过数小时的研究,我仍然不明白为什么它会加载这个图标而不是箭头(是的,我检查了可绘制的;)。感谢您提供的解决方案和提示

设置活动性:

public class SettingsActivity extends PreferenceActivity {

private AppCompatDelegate mDelegate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();
    getDelegate().onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

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

    Intent intent = getIntent();
    boolean hasConfiguredSettings = intent.getBooleanExtra(HAS_CONFIGURED_SETTINGS, false);

    if (hasConfiguredSettings) {
        toolbar.setTitle("Wähle deine Linien!");


    } else {
        // Back button in Toolbar
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(R.id.content, new SettingsFragment())
            .commit();
}

private void setSupportActionBar(Toolbar toolbar) {
    getDelegate().setSupportActionBar(toolbar);
}

private AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
        mDelegate = AppCompatDelegate.create(this, null);
    }
    return mDelegate;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Intent intent = getIntent();
    boolean hasConfiguredSettings = intent.getBooleanExtra("HAS_CONFIGURED_SETTINGS", false);
    if (hasConfiguredSettings) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_settings, menu);
        return true;
    }

    return false;
}
}
menu_settings.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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SettingsActivity">
<item
    android:id="@+id/action_checked"
    android:orderInCategory="101"
    android:title="@string/configuration_action_button"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_arrow_right"/>
</menu>

toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_height="wrap_content"
  android:layout_width="match_parent"
  android:background="@color/primary"
/>

工具栏随附于:

<include
    android:id="@+id/tool_bar"
    layout="@layout/toolbar" />

您需要使用而不是

替换以下行:

MenuInflater inflater = getMenuInflater();


对真正的图标是:
ic\u action\u arrow\u right
,看起来像这样:错误的图标看起来像工具栏右侧图片中的图标。它实际上看起来是这样的:。。。这就是所谓的烤肉串菜单;)@他指的是错误/正确的图标,但选错了词。我明白了。可能是正确的和错误的图标名称相反。因此,安卓
似乎
在交换它们。请查看这篇文章。
MenuInflater inflater = getDelegate().getMenuInflater();