日夜模式切换
来源:互联网 发布:淘宝g20公告 编辑:程序博客网 时间:2024/04/26 15:13
rea 文件下values 里面 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
<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>
values里建一个attrs.xml
<resources> <!--reference 引用--> <attr name="mainBackground" format="color|reference"></attr></resources>布局<Button android:id="@+id/bt_change" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="?attr/mainBackground" android:text="日夜间模式切换"/>
代码
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");
}
}
- 日夜模式切换
- 日夜切换两种模式
- 简单的日夜模式切换
- SlidingMenu以及日夜模式切换配置
- 安卓日夜模式的切换
- 开关按钮实现日夜模式切换
- 日夜模式
- 通过动态获取资源ID,执行接口回调-----来实现日夜模式切换
- Dialog默认背景的修改-切换日夜模式的Dialog背景
- 安卓实现日夜模式
- slidingmenustyle侧拉和主题日夜切换
- 又一个日夜
- 模式切换
- 3月28日夜
- 记,17个日夜
- 大江流日夜
- 那是我日夜思念
- linux模式切换等
- Dinic
- 解决ubuntu16.04搜狗拼音崩溃的问题
- cuDNN兼容性问题造成的caffe/mnist,py-faster-rcnn/demo运行结果错误
- TypeScript设计模式之备忘录、命令
- LeetCode刷题【Array】 Rotate Array
- 日夜模式切换
- Lua Development Tools 乱码问题!
- CSS权重
- 资源 | 从人脸识别到机器翻译:52个有用的机器学习和预测API
- itoa函数
- 【转载】Python实现树莓派WiFi断线自动重连
- 数据库相关-关系代数
- java环境变量配置
- 解决异常-ORA-01747 invalid user.table.column, table.column, or column specification