Android 切换日夜间模式
来源:互联网 发布:软件项目开发阶段 编辑:程序博客网 时间:2024/05/01 09:32
在attrs.xml文件中配置属性
<attr name="mainBackground" format="color"/>
在layout.xml文件中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="?mainBackground" android:orientation="vertical" > <Button android:id="@+id/btnSet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="夜间模式" /></LinearLayout>
在styles.xml文件中
<style name="DayTheme" parent="@android:style/Theme"> <item name="mainBackground">#ffffff</item> </style> <!-- 夜间模式 --> <style name="NightTheme" parent="@android:style/Theme"> <item name="mainBackground">#000000</item> </style>
在MainActivity中
public class MainActivity extends Activity implements OnClickListener { private Button btnSet; private boolean isnightMode = false;// 设置一个标记,表示是否为夜间模式 private String text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); showTheme();// 显示主题操作 setContentView(R.layout.activity_main);// 重新加在布局 initView();// 初始化控件 showText(); initSetOnClickListener();// 初始化点击事件 } private void showTheme() { isnightMode = SharedPreferencesUtils.getBoolean(this, "night_mode", "night_mode"); if (!isnightMode) { setTheme(R.style.DayTheme); } else { setTheme(R.style.NightTheme); } } private void showText() { if (!isnightMode) { text = "夜间模式"; } else { text = "日间模式"; } btnSet.setText(text); } private void initView() { btnSet = (Button) findViewById(R.id.btnSet); } private void initSetOnClickListener() { btnSet.setOnClickListener(this); } @Override public void onClick(View v) { if (!isnightMode) {// 表示日间模式 setTheme(R.style.NightTheme); } else { // 切换为日渐模式 setTheme(R.style.DayTheme); } isnightMode = !isnightMode;// 改变标记 // 保存 SharedPreferencesUtils.saveBoolean(this, "night_mode", "night_mode", isnightMode); setContentView(R.layout.activity_main);// 重新加在布局 initView(); showText(); initSetOnClickListener(); }}
说明:SharedPreferencesUtils是自己写好的工具类
0 0
- Android 切换日夜间模式
- Android日夜间模式切换
- Android日夜间切换模式
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- Android实现日夜间模式切换
- android日夜间模式切换动画
- Android 日夜间模式切换:ThemeDemo
- Android APP日夜间模式切换
- 韩顺平 javascript教学视频_学习笔记17_js超级玛丽小游戏
- java第一天学习回顾
- OC与Swift混编
- UI_tableView
- WebSocket 初探
- Android 切换日夜间模式
- oc中在控件上显示图片
- 论文笔记:Clustering is efficient for approximate maximum inner product search
- Palindrom Numbers
- UI组件之GridView实现图片浏览器
- 0.0与0.0f区别
- nova-compute Periodic tasks 机制
- 学习sqlite3(四)---SQLite中的高级SQL
- 机器人操作系统ROS教程(二) ROS总体框架