gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:29:07 25 4
gpt4 key购买 nike

在我的应用程序中,我想为工具栏设置自定义菜单。我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com