日夜间模式
来源:互联网 发布:小土豆编程软件 编辑:程序博客网 时间:2024/05/09 09:27
建立Arrts
<resources>
<!--reference 引用--><attr name="mainBackground" format="color|reference"></attr>
</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>
style
<!-- 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>
Activity
public class MainActivity extends AppCompatActivity {
//默认的日间模式
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 bt_change = (Button) findViewById(R.id.bt_change);
bt_change.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);
}
//恢复数据
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
theme = savedInstanceState.getInt("theme");
}
}
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式转换
- 日夜间模式
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式转换
- 日夜间模式
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式
- 日夜间模式
- bzoj3531 [JLOI2014]松鼠的新家
- Linux下复制与粘贴
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDE(代码待完善)
- 笨鸟后飞也不晚
- 开源 java CMS
- 日夜间模式
- 学习设计模式之模板模式
- 最大子序列和+最大子矩阵-动态规划-无
- 百万数据查询优化
- 06.Mybatis初级使用-Mapper映射器(三)
- Qt中的日期时间类
- 神经网络入门 ,源码5
- 学习笔记1
- windows上传版本到github