日夜间模式的切换
来源:互联网 发布:mhol捏脸数据 编辑:程序博客网 时间:2024/05/01 15:36
在values里的colors.xml里
<?xml version="1.0" encoding="utf-8"?><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>
然后在values里的styles.xml里
<resources> <!-- 日间模式 --> <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> <!-- 夜间模式 --> <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>
然后在values里创建attrs.xml
<?xml version="1.0" encoding="utf-8"?><resources> <!--reference 引用--> <attr name="mainBackground" format="color|reference"></attr></resources>在相对布局中或者线性布局中添加:
android:background="?attr/mainBackground"MainActivity里
//方法1
package bawei.com.riye;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private SharedPreferences sharedPreferences; private SharedPreferences.Editor edit; private Button button; //默认的日间模式 private int theme = R.style.AppTheme; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//恢复数据 做判空if (savedInstanceState != null) {theme = savedInstanceState.getInt("theme");//设置主题 此方法必须在setContentView()之前调用setTheme(theme); }//setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.riye);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//切换日夜间模式theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme;//重新创建recreate();} }); }//保存数据@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putInt("theme", theme); }//恢复数据@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState);theme = savedInstanceState.getInt("theme"); }}//方法2
package bawei.com.riye;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private SharedPreferences sharedPreferences; private SharedPreferences.Editor edit; private Button button; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);sharedPreferences = getSharedPreferences("jiangjun",MODE_PRIVATE);edit = sharedPreferences.edit();
boolean riye = sharedPreferences.getBoolean("riye", false);
if (riye){setTheme(R.style.NightAppTheme);}else {setTheme(R.style.AppTheme);//}setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.riye);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {boolean riye1 = sharedPreferences.getBoolean("riye", false);//if (riye1){//edit.putBoolean("riye",false);//edit.commit();//}else {//edit.putBoolean("riye",true);//edit.commit();//} //重新创建recreate();} }); }
0 0
- 日夜间模式的切换
- 日夜间模式的切换
- 日夜间模式的切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- Android 切换日夜间模式
- 轻松切换日夜间模式
- Android日夜间模式切换
- Android日夜间切换模式
- SQL语句
- 微信企业号OAuth2.0验证,微信企业号拍照上传,下载图片到自己的服务器
- 活动干货|基于Docker的DevOps实现
- json转对象参数不匹配问题
- ccf 画图
- 日夜间模式的切换
- 基于Node.js微信授权获取用户信息
- android自定义notification UI
- MATLAB 私人使用手册之画图
- PopupWindow的创建及点击空白处弹出窗dismiss掉
- LintCode Maximum Number in Mountain
- 插件化
- 匿名对象
- 精通Apache Flink读书笔记--5