日夜改变'PrefFragment`

时间:2017-06-25 06:33:16

标签: java

我的应用程序中存在更改主题DayNight的问题。 我正在使用PreferenceFragment

我可以改变主题,但有时可以看不到正确的颜色( 这是我的片段:

public class MyPreferenceFragment extends PreferenceFragment
    implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String NIGHT_MODE = "nightMode" ;
public static final String MAP_SWICH = "switch_preference_1" ;
public static final String Disable = "disabled";
public static final String Enable = "enabled";
private SharedPreferences preferences;
String versionName;
ListPreference nightSwich;


@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    nightSwich = (ListPreference)findPreference(NIGHT_MODE);

}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals(NIGHT_MODE)) {
        preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        nightSwich = (ListPreference)findPreference(NIGHT_MODE);
        String nightMode = preferences.getString(NIGHT_MODE, Disable);
        switch (nightMode) {
            case Disable:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case Enable:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
        }

       startActivity(new Intent(getActivity(), Drawer_main.class));
    }

但这是我的主要活动:

private static final String NIGHT_MODE = "nightMode" ;
public static final String Disable = "disabled";
public static final String Enable = "enabled";
private SharedPreferences mSettings;
private int mCounter;



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


    setContentView(R.layout.activity_drawer_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    toolbar.setCollapsible(true);
    initDrawer(toolbar);
    prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());


    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String nightMode = preferences.getString(NIGHT_MODE, "");
    int themeMode = AppCompatDelegate.getDefaultNightMode();

    //mSettings = getSharedPreferences(NIGHT_MODE, Context.MODE_PRIVATE);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    firstStartCheck();

    if (savedInstanceState == null) {
        Fragment fragment = null;

        switch (nightMode)
        {
            case Disable:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case Enable:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
        }
        Class fragmentClass;

        fragmentClass = Fragment_taxi_0.class;
        try {
            fragment = (Fragment) fragmentClass
                    .newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .replace(R.id.container22, fragment)
                .commit();
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题很明显 为什么这不会改变通过属性drawable xml

注册的颜色

如果要通过颜色xml立即解决一切正常