android - 工具栏设置错误的图标

标签 android android-layout android-toolbar

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

False Icon 1 False icon after click on three-point menu

设置 Activity :

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;
}
}

菜单设置.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>

工具栏.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" />

最佳答案

您需要使用 AppCompatDelegate#getMenuInflater而不是 Activity#getMenuInflater() .

替换下面一行:

MenuInflater inflater = getMenuInflater();

MenuInflater inflater = getDelegate().getMenuInflater();

关于android - 工具栏设置错误的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32298637/

相关文章:

android - 从多个复选框中选择一个复选框并让用户选中复选框

android - 膨胀布局完成事件中的所有 View

android-layout - 来自 View 适配器的无条件布局膨胀。 Kotlin

android - 转到第二行时如何减小文本大小?

android - 在 Android Studio 中未修改的新创建项目中,工具栏和状态栏在选项卡式 Activity 中重叠

java - 使用数组时出现 NullPointerException

android - 将运行时依赖项打包到 aar 库中

android - 如何使用 RelativeLayout 根的合并标签?

android - 我如何将溢出菜单放在工具栏下方而不是溢出菜单与应用栏重叠

android - 更改滚动 Android 上的工具栏 alpha