日夜间模式切换
来源:互联网 发布:沸点滑板怎么样知乎 编辑:程序博客网 时间:2024/05/01 18:57
colors.xml
<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.xml
<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"> <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>
自创: attrs.xml
<resources> <attr name="mainBackground" format="color|reference"></attr></resources>
// 默认是日间模式private int theme = R.style.AppTheme;
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(savedInstanceState != null){ theme = savedInstanceState.getInt("theme"); setTheme(theme); }
Button btn_theme = (Button) findViewById(R.id.btn_theme);btn_theme.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme; Toast.makeText(HomePage.this, "点击", Toast.LENGTH_SHORT).show(); HomePage.this.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");}
布局
android:background="?attr/mainBackground"
0 1
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- Android 切换日夜间模式
- 日夜间模式的切换
- 轻松切换日夜间模式
- 日夜间模式的切换
- 日夜间模式的切换
- Android日夜间模式切换
- Android日夜间切换模式
- 安卓危险权限
- 项目实战:超实用小工具isFastClick解决重复点击按钮问题
- 设计模式的原则
- Mac下Android Studio中获取SHA1和MD5
- React Native-随笔分类
- 日夜间模式切换
- [JavaScript]JS中常遇到的浏览器兼容问题和解决方法
- 七、ngTouch模块
- 欢迎使用CSDN-markdown编辑器
- repo_git 常用命令
- Valgrind工具:Linux 平台中调试 C C++ 内存泄漏
- 短信验证
- 选择排序 java实现
- 没有任何秘密的 API:Vulkan* 简介第 0 部分:前言