安卓设置夜间模式和正常模式
来源:互联网 发布:python编程 pdf 编辑:程序博客网 时间:2024/06/15 20:21
修改theme:
<!--白天主题--><style name="DayTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="clockBackground">@android:color/white</item> <item name="clockTextColor">@android:color/black</item></style><!--夜间主题--><style name="NightTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="colorPrimary">@color/color3F3F3F</item> <item name="colorPrimaryDark">@color/color3A3A3A</item> <item name="colorAccent">@color/color868686</item> <item name="clockBackground">@color/color3F3F3F</item> <item name="clockTextColor">@color/color8A9599</item></style>
添加attr.xml文件:
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="clockBackground" format="color"/> <attr name="clockTextColor" format="color"/></resources>
如果要改变布局中字体或者背景色,需要让该布局的设置与当前下的主题样式一致,就是在布局中添加
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="?attr/clockBackground" >
<Button android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/bt_in" android:gravity="center" android:onClick="myClick" android:textColor="?attr/clockTextColor" android:text="注册" />
后面就是在java代码的处理了:
MainActivity类中:
private boolean isNight=true;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); initTheme(); setContentView(R.layout.activity_main);}
private void initTheme() { if (isNight) { setTheme(R.style.NightTheme); } else { setTheme(R.style.DayTheme); }}/** * 切换主题设置 */private void toggleThemeSetting() { if (!isNight) { setTheme(R.style.NightTheme); isNight=true; } else { setTheme(R.style.DayTheme); isNight=false; }}public void myClick(View v) { switch (v.getId()) { case R.id.bt_in: toggleThemeSetting(); refreshUI(); break; case R.id.bt_out: toggleThemeSetting();refreshUI();break; } }private void refreshUI() { TypedValue background = new TypedValue();//背景色 TypedValue textColor = new TypedValue();//字体颜色 Resources.Theme theme = getTheme(); theme.resolveAttribute(R.attr.clockBackground, background, true); theme.resolveAttribute(R.attr.clockTextColor, textColor, true); ll.setBackgroundResource(background.resourceId);//将需要改变的样式进行修改后面的就是改变样式了,根据自己喜好吧。。}
0 0
- 安卓设置夜间模式和正常模式
- UiModeManager设置夜间模式和行车模式
- android设置白天模式和夜间模式
- 安卓夜间模式的实现
- 设置App夜间模式
- android 夜间模式设置
- 设置日间夜间模式
- 设置webView 的 夜间模式
- 设置简单的夜间模式
- 安卓开发之样式和主题的使用与夜间/白天模式的动态转换
- 系统自带方法设置日间和夜间模式切换
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 夜间模式
- 基于maven的ssh框架一步一步搭建
- MyBatis 官方文档学习3---动态 SQL
- MySql启动出现The server quit without updating PID file错误解决过程
- 【Docker江湖】之创建Web应用服务镜像——Apache/Nginx
- 【Linux4.1.12源码分析】协议栈报文接收之传输层处理分析(UDP)
- 安卓设置夜间模式和正常模式
- HTML学习小记4
- 前端日记——16.10.30
- python(十二)上:mysql安装使用详细教程
- unreal3的viewport和client
- <设计模式20>状态模式
- servlet/filter/listener/interceptor区别与联系
- linux驱动开发:平台设备驱动框架
- 最短路-Floyd算法