private int theme=R.style.AppTheme;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { theme=savedInstanceState.getInt("theme"); setTheme(theme); } setContentView(R.layout.activity_shou); sp = getSharedPreferences("s", 0); edit = sp.edit(); flag = sp.getBoolean("flag", true); initView(); //getSharedPreferences传值 if (!flag) { String iconurl = sp.getString("iconurl", "iconurl"); String name = sp.getString("name", "name"); ImageOptions options = new ImageOptions.Builder().setCircular(true).setCrop(true).setSize(100, 100).setLoadingDrawableId(R.mipmap.ic_launcher).build(); //ImageOptions options1 = new ImageOptions.Builder().setCircular(true).setCrop(true).setSize(40, 40).setLoadingDrawableId(R.mipmap.ic_launcher).build(); x.image().bind(touxiangYin, iconurl,options); //x.image().bind(cehuaimg, iconurl,options1); textName.setText(name); qqdenglu.setVisibility(View.GONE); shoujidenglu.setVisibility(View.GONE); weibodenglu.setVisibility(View.GONE); touxiangYin.setVisibility(View.VISIBLE); } if(theme== R.style.NightAppTheme){ yejianImg.setImageResource(R.mipmap.nighticon_profile); textYejian.setText("夜间"); }else { yejianImg.setImageResource(R.mipmap.dayicon_profile); textYejian.setText("日间"); } }//日夜模式转换 public void getRiYeZhuan() { yejianImg = (ImageView) findViewById(R.id.yejianImg); textYejian = (TextView) findViewById(R.id.textYejian); yejianImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { theme=(theme==R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme; recreate(); } }); } //保存数据 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("theme", theme); }
//attrs文件<resources> <!--reference 引用--> <attr name="mainBackground" format="color|reference"></attr> </resources>
//colors文件<resources> <!--日间模式下的颜色--> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <!--夜间模式下的颜色--> <color name="nightColorPrimary">#3b3b3b</color> <color name="nightColorPrimaryDark">#383838</color> <color name="nightColorAccent">#a72b55</color> </resources>
//styles文件<resources> <!-- Base application theme. 日间模式--> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:textColor">@android:color/black</item> <item name="mainBackground">@android:color/white</item> </style> <!-- Base application theme. 夜间模式--> <style name="NightAppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/nightColorPrimary</item> <item name="colorPrimaryDark">@color/nightColorPrimaryDark</item> <item name="colorAccent">@color/nightColorAccent</item> <item name="android:textColor">@android:color/white</item> <item name="mainBackground">@color/nightColorPrimaryDark</item> </style> </resources>
//布局文件加android:background="?attr/mainBackground"
日夜间模式转换
最新推荐文章于 2022-09-07 16:08:21 发布