日夜间模式的切换
来源:互联网 发布:阿里云校园扶持 编辑:程序博客网 时间:2024/05/01 05:09
1.values文件夹下属性的配置android:background="?attr/mainBackground"
--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>
--mainBackground.xml
<resources> <attr name="mainBackground" format="color|reference"></attr></resources>
--styles.xml 配置两套主题
<!-- 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><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>
2.在layout布局中引用
android:background="?attr/mainBackground"
3.代码中
public class MainActivity extends AppCompatActivity { // 默认是日间模式 private int theme = R.style.AppTheme; @Override protected void onCreate(Bundle savedInstanceState) { // 判断是否有主题存储 if(savedInstanceState != null){ theme = savedInstanceState.getInt("theme");
//设置主题 此方法必须在setContentView()方法之前调用
setTheme(theme); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn_theme = (Button) findViewById(R.id.button); btn_theme.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { theme = (theme == R.style.AppTheme) ? R.style.NightAppTheme : R.style.AppTheme; MainActivity.this.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"); }}
0 0
- 日夜间模式的切换
- 日夜间模式的切换
- 日夜间模式的切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- 日夜间模式切换
- Android 切换日夜间模式
- 轻松切换日夜间模式
- Android日夜间模式切换
- Android日夜间切换模式
- 奥威软件受邀长三角CIO高峰论坛 共探信息未来
- Android intent页面跳转
- ios 应用间通信的规范
- android的下载和更新
- quartz教程-触发器Trigger
- 日夜间模式的切换
- Python+Selenium练习篇之5-利用partial link text定位元素
- php-china.org 的开源项目安装
- 文章标题
- ArcGIS API For Javascript事件处理代码
- distinct中第一条和最后一条
- 模拟http请求的网页调试工具
- python学习-关系数据库
- ireport+jsp基于jasperReport 实现(后台map数据源)导出报表至web页面 二