[英]Howto change the Icon MenuItem android app
我想在 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();
}
我的菜單。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>
我怎樣才能意識到圖標會根據日夜模式而改變?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.