【问题标题】:Howto change the Icon MenuItem android app如何更改图标 MenuItem android 应用程序
【发布时间】:2022-01-29 19:55:06
【问题描述】:

我想在 MenuItem 中实现昼夜模式切换。如果选择白天模式,则应显示月亮,如果选择夜间模式,则应显示太阳。我将设置保存在 SharedPreferences 中,并希望在启动应用程序时再次加载它们。

在我定义的 MainActivity-Class 中

  public static final String NIGHT_MODE = "night_mode";
  private boolean safedNightMode;

这是我加载 SharedPreferences 的 onCreate 方法

 protected void onCreate(Bundle savedInstanceState) {

        requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
        Objects.requireNonNull(getSupportActionBar()).setLogo(R.mipmap.logo_psc_round);
        getSupportActionBar().setDisplayUseLogoEnabled(false);

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

        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
        safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE,false);
    
        if (safedNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }

我的 onCreateOptionsMenu 和我的 onOtionsItemSelected 方法

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_clean:
                openDialog();
                return true;
            case R.id.item_dn_switch:
                switchDayNightMode();
                return true;
            case R.id.item_recipe:
                Intent intentRecipe = new Intent(this, RecipeActivity.class);
                startActivity(intentRecipe);
                return true;
            case R.id.item_about:
                Intent intentAbout = new Intent(this, AboutActivity.class);
                startActivity(intentAbout);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

当项目被选中时,我的 switchDayNightMode 方法将被调用

public void switchDayNightMode() {
        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS,MODE_PRIVATE);
        safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE,false);
        nightMode = safedNightMode;
        if (safedNightMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            nightMode = false;
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            nightMode = true;
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(NIGHT_MODE, nightMode);
        editor.apply();
    }

我的 menu.xml 在这里。相关项是android:id="@+id/item_dn_switch"。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/item_clean"
        android:title="@string/txt_menu_item_counter_reset"
        android:icon="@drawable/ic_baseline_delete_forever_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_dn_switch"
        android:title="@string/txt_menu_item_dnswitch"
        android:icon="@drawable/ic_baseline_mode_night_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_recipe"
        android:title="@string/txt_menu_item_recipe"
        android:icon="@drawable/ic_baseline_list_24"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/item_about"
        android:title="@string/about"
        android:icon="@drawable/ic_baseline_contact_support_24"
        app:showAsAction="ifRoom"/>
</menu>

我如何才能意识到图标会根据昼夜模式发生变化?

【问题讨论】:

    标签: java android android-layout menuitem


    【解决方案1】:

    您可以通过创建带有夜间主题图标的文件夹来将图标更改为夜间。创建此文件夹(在 res 文件夹上单击右键)并在屏幕上设置如下值。创建后,您应该将夜间图标移动到这个新文件夹中。图标应该会自动切换到夜间主题。

    【讨论】:

    • 好的,我明白了。所以这是天图标,我在布局中定义什么?对吗?
    • 问的不同。那么我在 menu.xml 中定义什么
    • @amarradi 创建第二个文件夹后,您会得到第一个名为 drawable 的文件夹,您可以在其中存储白天图标,第二个文件夹称为 drawable-night,您可以在其中存储夜间图标。两个文件夹中的图标名称必须相同。您不会更改 menu.xml。
    • 非常感谢。谢谢,就这样。
    猜你喜欢
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多