How to enable night mode programmatically?

后端 未结 3 458
别跟我提以往
别跟我提以往 2020-12-15 06:44

I am looking for a way to enable night mode programmatically with an Android code:

public static void setNightMode(Context target , boolean state){

    UiM         


        
3条回答
  •  春和景丽
    2020-12-15 07:30

    NightOwl has its own implementation for switching day/night mode on Android. Getting started with NightOwl is super easy. Here's a code snippet:

    Init the NightOwl in Application class,

    NightOwl.builder().defaultMode(0).create();
    

    Call three method in your Activity class,

    public class DemoActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // step1 before super.onCreate
            NightOwl.owlBeforeCreate(this);
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_demo);
    
            // step2 after setContentView
            NightOwl.owlAfterCreate(this);
    
            // write your code
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            // step3 onResume
            NightOwl.owlResume(this);
        }
    
    }
    

    Switch skin everywhere as you like,

    View v = findViewById(R.id.button);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NightOwl.owlNewDress(SettingActivity.this);
        }
    });
    

提交回复
热议问题