夜间模式导航抽屉Android Studio

时间:2019-02-20 00:58:02

标签: android

我在将所有活动都设为夜间模式时遇到了问题,即使从该平台上我也尝试了很多解决方案。但是我似乎并没有安静地得到答案。有些零件进入夜间模式,而其他零件则没有。这是我的代码块,这是图片

The Problem

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
         Shared_Preferences sharedPref;
        sharedPref = new Shared_Preferences(this);

        if (sharedPref.loadNightModeState()) {
            setTheme(R.style.darktheme);
            //restartApp();
            //getSupportActionBar().setBackgroundDrawable(getDrawable(R.drawable.actionbar));
            //actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.BackgroundLight));
        } else setTheme(R.style.AppTheme);
        //restartApp();

        setContentView(R.layout.activity_main);



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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

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



    }

    public void setActionBarTitle(String title){
        getSupportActionBar().setTitle(title);
    }

    private void showHome(){
        fragment = new HomeFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.fragment_container, fragment, fragment.getTag()).commit();
    }

    @Override
    public void onBackPressed() {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if(fragment instanceof HomeFragment){
                super.onBackPressed();
            }
            else{
                showHome();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.


        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
      Intent settings = new Intent(this, SettingsActivity.class);
      startActivity(settings);
        }

        return super.onOptionsItemSelected(item);
    }

    Fragment fragment = null;

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item. getItemId();

        if (id == R.id.nav_home) {

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                   fragmentTransaction.replace(R.id.fragment_container, new HomeFragment());
                   fragmentTransaction.commit();
        } else if (id == R.id.nav_ministry) {

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, new MinistryFragment());
            fragmentTransaction.commit();

        } else if (id == R.id.nav_contact) {

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, new ContactFragment());
            fragmentTransaction.commit();

        } else if (id == R.id.nav_about) {

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, new AboutFragment());
            fragmentTransaction.commit();

        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}

@Override
protected void onCreate(Bundle savedInstanceState){

    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    Switch switchCompat;
    final Shared_Preferences sharedPref;

    sharedPref = new Shared_Preferences(this);

    if (sharedPref.loadNightModeState()) {
        setTheme(R.style.darktheme);
    } else setTheme(R.style.AppTheme);

    setContentView(R.layout.activity_settings);
    switchCompat = (Switch)findViewById(R.id.myswitch);
    if (sharedPref.loadNightModeState()) {
        switchCompat.setChecked(true);
    }else switchCompat.setChecked(false);

    switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                sharedPref.setNightModeState(true);
                restartApp();
            } else {
                sharedPref.setNightModeState(false);
                restartApp();
            }
        }
    });
}




private void restartApp() {
    Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
    startActivity(intent);
    finish();
}
}

activity_settings

0 个答案:

没有答案