Android 日夜间切换Demo
这是其中一种实现模式,也是比较麻烦的一种,首先写布局,不多说上代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.day_night_demo2.MainActivity"><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="?attr/textContent"/><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="Hello World!" android:textColor="?attr/textColorValue" /><TextView android:textColor="?attr/textColorValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Hello World!" /><TextView android:textColor="?attr/textColorValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Hello World!" /> </LinearLayout>-------------------------------------------------------------------------------------------------------
res资源文件夹下values文件夹下创建attrs.xml资源文件
<?xml version="1.0" encoding="utf-8"?> <resources><attr name="textColorValue" format="color"></attr><attr name="textContent" format="string"></attr> </resources>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
colors.xml
<color name="background">#252a2e</color> <color name="unablebtn">#dcdcdc</color> <color name="dark_bg">#505050</color> <color name="light">#ECECEC</color> <color name="white">#FFFFFF</color> <color name="black">#000000</color> <color name="green">#05D992</color> <color name="zise">#E5004F</color> <color name="dark_bg1">#414141</color> <color name="pink">#FF5877</color> <color name="yellow">#FFFF00</color>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Strings.xml
<string name="changge_to_night">切换成夜间模式</string> <string name="changge_to_day">切换成日间模式</string>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
styles.xml
<!-- Base application theme. 白天的模式 --> <style name="day_theme" 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:windowBackground">@color/white</item><!--日间模式对应的字体颜色 和日间模式对应的文本内容--> <item name="textColorValue">@color/black</item><item name="textContent">@string/changge_to_night</item></style><!-- Base application theme. 夜晚的模式 --> <style name="night_theme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --> <item name="colorPrimary">@color/dark_bg</item><item name="colorPrimaryDark">@color/dark_bg</item><item name="colorAccent">@color/dark_bg</item><item name="android:windowBackground">@color/dark_bg</item><!--夜间模式对应的字体颜色 和夜间模式对应的文本内容--> <item name="textColorValue">@color/white</item><item name="textContent">@string/changge_to_day</item> </style>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在res文件夹下创建anim文件夹用来存放动画,为了使模式切换更自然,使用补间动画
sliding_in.xml
<?xml version="1.0" encoding="utf-8"?> <alphaxmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0"></alpha>
sliding_out.xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:toAlpha="0.0" android:fromAlpha="1.0"></alpha>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面就是具体实现代码了
package com.example.day_night_demo2;import android.app.Activity; import android.content.Intent;/** * Created by lenovo on 2017/9/5. */ public class ThemeUtil {//我当前的主题 private static int theme = 0;//日间模式主题 private static final int DAY_THEME = 0;//夜间模式主题 private static final int NIGHT_THEME = 1;public static void onActivityCreatedSetTheme(Activity activity) {switch (theme) {case DAY_THEME:activity.setTheme(R.style.day_theme);break;case NIGHT_THEME:activity.setTheme(R.style.night_theme);break;}}//点击按钮改变对应的主题 public static void ChangeCurrentTheme(Activity activity) {//改变当前主题的theme变量 switch (theme) {case DAY_THEME:theme = NIGHT_THEME;break;case NIGHT_THEME:theme = DAY_THEME;break;}//重启这个activity activity.finish();activity.overridePendingTransition(R.anim.sliding_in,R.anim.sliding_out);activity.startActivity(new Intent(activity,activity.getClass()));} }
=========================================================================================
package com.example.day_night_demo2;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View;public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置对应的主题 ,在ui创建好之后设置主题无效,所以要放到setContentView()方法前面setTheme() ThemeUtil.onActivityCreatedSetTheme(this);setContentView(R.layout.activity_main);}public void onClick(View v){ThemeUtil.ChangeCurrentTheme(this);} }
转载于:https://www.cnblogs.com/powersen/p/7524584.html
Android 日夜间切换Demo相关推荐
- android日夜间模式切换动画
转载请说明出处http://blog.csdn.net/asd2603934/article/details/50541701 在一些应用中需要有日夜间模式,当切换模式之后,如果不给点过渡动画,那效果 ...
- android日夜间模式切换
一 日夜间模式方法 public class BaseActivity extends Activity { protected View mCoverView; @Override pu ...
- android 切换摄像头加动画,android前后摄像头切换
android前后摄像头切换demo,startview后的页面上有切换.拍照两个按钮,拍照后照片未保存,需要保存的请看下方注释,实现了拍照.聚焦功能,以下是CameraPreview.java源码: ...
- android自动夜间模式,Android实现日夜间模式的深入理解
在本篇文章中给出了三种实现日间/夜间模式切换的方案,三种方案综合起来可能导致文章的篇幅过长,请耐心阅读. 1.使用 setTheme的方法让 Activity重新设置主题: 2.设置 Android ...
- android夜间切换失败,Android夜间模式切换记录
使用AndroidSDK自带的support lib来实现APP日间/夜间模式的切换 添加依赖包appcompat-v7 在app module的gradle中依赖appcompat库,版本最低是23 ...
- android设置夜间模式切换,android主题切换(简单的白/夜间模式的切换)
效果图: 实现: android:layout_width="match_parent" android:layout_height="match_parent" ...
- Android中夜间模式的三种实现方式
参考:https://www.jianshu.com/p/f3aaed57fa15 在本篇文章中给出了三种实现日间/夜间模式切换的方案: 使用 setTheme 的方法让 Activity 重新设置主 ...
- android自动夜间模式,夜晚的故事(android夜间模式实现)
夜幕降临,他走在马路上,回想着今天发生的一切,他不敢相信事情就这样发生了.他最终还是决定拨打那个电话,掏出手机,解锁屏幕,突然一道强光从屏幕里毫无预兆的发射出来.他一个踉跄倒在了马路中央.而他身后伴随 ...
- android所有需要的Demo地址
注:打开请贴网址,有些直接通过链接打开的不正确. 1.相对布局实例 http://kukuqiu.iteye.com/blog/1018396 2.Log图文详解(Log.v,Log.d,Log.i, ...
最新文章
- linux清空文件内容
- H - 拦截导弹 OpenJ_Bailian - 2945(dp动态规划)
- python怎么安装pin库_Python库之numpy库的安装教程
- android+委托列表,在Android适配器中使用委托者模式
- centos7删除符号链接_如何在 Linux 上查找和删除损坏的符号链接 | Linux 中国
- MFC中 给基于CFormView的单文档添加背景图片
- 3dmax模型在web端
- 狮子鱼小程序独立版安装配置教程
- 《韩立刚计算机网络》第二章
- 服务器引导损坏怎么处理,硬盘主引导记录(MBR)损坏,一个命令轻松解决问题...
- ASCII字符绘图网站推荐及使用Python绘制ASCII字符画
- 丹佛大学计算机专业,丹佛大学计算机工程排名第79(2020年TFE美国排名)
- 如何关闭百度网盘超级会员自动续费服务?
- 计算机控制矢量网络分析仪,完全独立的HF(短波)软件定义收发器和矢量网络分析仪设计...
- tcp支持浏览器websocket协议
- python输入一个英文句子、统计单词个数_C语言编程求一个英文句子中的单词数和最长单词的位置、长度及输出这个单词。c++编程 从键盘输入一个英文...
- 阿哈c语言教程pdf,C++教程-完整版.pdf
- 计算机报名照片没有重命名,照片重命名怎么弄
- 我的四周年创作纪念日
- 肉眼看不见的信息传递圈层
热门文章
- python实现多线程的三种方法threading.Thread(模块)的继承实现和函数实现;以及concurrent.futures模块的线程池实现
- JAVAEE框架之Spring AOP
- 第04篇 JDK版本导致Unsupported major.minor version 52.0 error
- 数组转json去除首尾引号_诞生于JavaScript,json的前世今生
- python 多数据输出到txt_python-BeautifulSoup输出到.txt文件
- 开源API网关Kong基本介绍和安装验证
- Python Demo 02 蒙特卡罗方法输出e
- 决策树数学原理(ID3,c4.5,cart算法)
- 修改Apache的默认站点目录的方法,需要的朋友可以参考下
- 借助码云快速安装 golang.org/x/lint/golint 和 golang.org/x/tools/ 等依赖