有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景。同时下一次启动程序,当前设置依然有效。

根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过Android Them + SharedPreferences 来实现需求。Them用于存放设置的每一种样式,并应用于程序中,SharedPreferences用于记住程序当前的样式,根据SharedPreferences的内容来设置程序的样式,实现下次启动能够oncreat当前的样式设置。

这里的Them比较简单,只是定义了字体颜色和页面背景颜色。在res/values/styles.xml 文件中增加Them主题

    <style name="FirstThem">
<item name="android:textColor">@color/FirstThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/FirstThemBackgroundColor</item> <!-- 窗口背景 -->
</style> <style name="SecondThem">
<item name="android:textColor">@color/SecondThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/SecondThemBackgroundColor</item> <!-- 窗口背景 -->
</style> <style name="ThirdThem">
<item name="android:textColor">@color/ThirdThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/ThirdThemBackgroundColor</item> <!-- 窗口背景 -->
</style>

然后在MainActivity.java中创建SharedPreferences来记录样式的状态

    private void SharePreference() {
sharePrefences=this.getSharedPreferences("config",Context.MODE_WORLD_READABLE
| Context.MODE_WORLD_WRITEABLE);
editor=sharePrefences.edit();
boolean isThem = sharePrefences.getBoolean("isThem", false);
int Them = sharePrefences.getInt("Them",0);//config不存在时返回0
if(isThem){
if(Them==1){
setTheme(R.style.FirstThem);
}else if(Them==2){
setTheme(R.style.SecondThem);
}else if(Them==3){
setTheme(R.style.ThirdThem);
}
}else{//sharePrefences不存在是使用默认主题
setTheme(R.style.FirstThem);
}
}

有两个比较值得注意的地方是:

1、设置主题时,setTheme(R.style.FirstThem);一定要放在setContentView(R.layout.activity_main);前,否则无效。

setTheme(R.style.FirstThem);
setContentView(R.layout.activity_main);

2、要所有页面的字体颜色和背景能够根据Them去改变,那么布局文件中的目标控件都不能设置android:textcolor,以及android:background.否则控件的android:textcolor,android:background属性会将Them的设置覆盖。

下面是一个demo的完整代码:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <Button
android:id="@+id/FirstThem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="样式一"
/> <Button
android:id="@+id/SecondThem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="样式二"
/> <Button
android:id="@+id/ThirdThem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="样式三"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:textSize="30sp"
android:text="ABCDEFG"
/> </LinearLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
private Button FirstThemButton;
private Button SecondThemButton;
private Button ThirdThemButton;
private SharedPreferences sharePrefences;
private Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharePreference();
setContentView(R.layout.activity_main);
InitView();
FirstThemButton=(Button) findViewById(R.id.FirstThem);
SecondThemButton=(Button) findViewById(R.id.SecondThem);
SecondThemButton=(Button) findViewById(R.id.ThirdThem);
} private void SharePreference() {
sharePrefences=this.getSharedPreferences("config",Context.MODE_WORLD_READABLE
| Context.MODE_WORLD_WRITEABLE);
editor=sharePrefences.edit();
boolean isThem = sharePrefences.getBoolean("isThem", false);
int Them = sharePrefences.getInt("Them",0);//config不存在时返回0
if(isThem){
if(Them==1){
setTheme(R.style.FirstThem);
}else if(Them==2){
setTheme(R.style.SecondThem);
}else if(Them==3){
setTheme(R.style.ThirdThem);
}
}else{//sharePrefences不存在是使用默认主题
setTheme(R.style.FirstThem);
}
} private void InitView() {
FirstThemButton=(Button) findViewById(R.id.FirstThem);
SecondThemButton=(Button) findViewById(R.id.SecondThem);
ThirdThemButton=(Button) findViewById(R.id.ThirdThem);
FirstThemButton.setOnClickListener(this);
SecondThemButton.setOnClickListener(this);
ThirdThemButton.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.FirstThem:
editor.putBoolean("isThem", true);
editor.putInt("Them", 1);
editor.commit();
Intent intent1=new Intent(this,MainActivity.class);
startActivity(intent1);
break;
case R.id.SecondThem:
editor.putBoolean("isThem", true);
editor.putInt("Them",2);
editor.commit();
Intent intent2=new Intent(this,MainActivity.class);
startActivity(intent2);
break;
case R.id.ThirdThem:
editor.putBoolean("isThem", true);
editor.putInt("Them", 3);
editor.commit();
Intent intent3=new Intent(this,MainActivity.class);
startActivity(intent3);
break;
default:
break;
} } }

styles.xml

<resources>

    <!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style> <style name="FirstThem">
<item name="android:textColor">@color/FirstThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/FirstThemBackgroundColor</item> <!-- 窗口背景 -->
</style> <style name="SecondThem">
<item name="android:textColor">@color/SecondThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/SecondThemBackgroundColor</item> <!-- 窗口背景 -->
</style> <style name="ThirdThem">
<item name="android:textColor">@color/ThirdThemTextColor</item> <!-- 字体颜色 -->
<item name="android:windowBackground">@color/ThirdThemBackgroundColor</item> <!-- 窗口背景 -->
</style> </resources>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="FirstThemTextColor">#000000</color>
<color name="FirstThemBackgroundColor">#FFFFFF</color>
<color name="SecondThemTextColor">#AAAAAA</color>
<color name="SecondThemBackgroundColor">#EEBBEE</color>
<color name="ThirdThemTextColor">#CCCCCC</color>
<color name="ThirdThemBackgroundColor">#AAAADD</color>
</resources>

Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景的更多相关文章

  1. Android NumberPicker 修改分割线颜色和高度及字体颜色大小

    (1)重写NumberPicker已达到修改显示字体颜色大小 public class TextColorNumberPicker extends NumberPicker { public Text ...

  2. Android SearchView 自定义SearchIcon和字体颜色大小

    自定义SearchView的搜索图标和字体属性相对复杂一些,记下来. 一.自定义SearchIcon 1.API版本低于21:版本小于21时,要修改SearchIcon比较复杂,需要先获取到Searc ...

  3. python设置图片背景和设置字体颜色大小

    # -*- coding: utf-8 -*- """ Created on Wed Dec 11 22:37:30 2019 @author: Dell "& ...

  4. 关于UIAlertAction如何修改sheet上的字体颜色

    相信很多程序员都会遇到需求是这样的: 但是你发现无论怎么设置cancel和Destructive都无法让红色字体移动到下面取消按钮上: 其实之前一直用错,用了ios9之前的UIActionSheet这 ...

  5. android自定义控件实现TextView按下后字体颜色改变

    今天跟大家分享一下Android自定义控件入门,先介绍一个简单的效果TextView,按下改变字体颜色,后期慢慢扩展更强大的功能 直接看图片             第一张是按下后截的图,功能很简单, ...

  6. C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数

    控制台程序(命令行程序)设置窗口宽度高度,如下代码: Console.WriteLine(Console.WindowHeight); Console.WriteLine(Console.Buffer ...

  7. 如何修改Zend Studio代码字体和大小

    Zend Studio的默认字体非常小,看起来很费神,这里教大家怎么修改它的字体和大小.   工具/原料   Zend Studio 方法/步骤     Window-->preferences ...

  8. WPS修改批注部分的字体颜色?

    今天遇到一个问题,就是复制文档的时候有几块红色字体想改成黑色,怎么也改不成功,通过修改字体颜色无效,通过百度找到了解决方法记录一下. 解决方法 审阅--显示标记--点击插入和删除(去掉前面的对钩即可) ...

  9. NPOI 修改指定单元格字体颜色

    //创建一个字体颜色 IFont font = hssfworkbook.CreateFont(); //红色 font.Color = HSSFColor.Red.Index; //样式 ICell ...

随机推荐

  1. python对目录下的文件进行 多条件排序

    在进入正题之前,先介绍一下基础知识: 1.sort(),方法:就是对列表内容进行正向排序,直接在原列表进行修改,返回的是修改后的列表 lists =[1, 5, 10, 8, 6]lists.sort ...

  2. drf 简介以及部分源码分析

    目录 复习 drf框架 全称:django-rest framework 知识点 接口 restful接口规范 基于restful规范的原生Django接口 主路由:url.py api组件的子路由: ...

  3. eNSP 交换机 路由器 PC 互连设计/实现

    0.实验目的 1.掌握网络设计的原理与步骤: 2.掌握IP分配.网关设置原则: 3.了解路由协议的作用,掌握网络互联设备的作用和配置. 1.实验环境 环境:eNSP模拟器 版本信息:1.3.00.10 ...

  4. 动态规划-Minimum Cost to Merge Stones

    2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...

  5. 走近源码:Redis如何清除过期key

    "叮--",美好的周六就这么被一阵钉钉消息吵醒了. 业务组的同学告诉我说很多用户的帐号今天被强制下线.我们的帐号系统正常的逻辑是用户登录一次后,token的有效期可以维持一天的时间 ...

  6. FaceBook 发布星际争霸最大 AI 数据集

    简介 我们刚发布了最大的星际争霸:Brood War 重播数据集,有 65646 个游戏.完整的数据集经过压缩之后有 365 GB,1535 million 帧,和 496 million 操作动作. ...

  7. iOS 图片加载和处理

    一.图片显示 图片的显示分为三步:加载.解码.渲染.解码和渲染是由 UIKit 进行,通常我们操作的只有加载. 以 UIImageView 为例.当其显示在屏幕上时,需要 UIImage 作为数据源. ...

  8. [vios1023]维多利亚的舞会3<强联通分量tarjan>

    题目链接:https://vijos.org/p/1023 最近在练强联通分量,当然学的是tarjan算法 而这一道题虽然打着难度为3,且是tarjan算法的裸题出没在vijos里面 但其实并不是纯粹 ...

  9. Java序列化机制剖析

    本文转载自longdick的博文<Java序列化算法透析>,原文地址:http://longdick.iteye.com Java序列化算法透析 Serialization(序列化)是一种 ...

  10. 系统警告,Bronya请求支援(两遍最短路)

    系统警告,Bronya请求支援 Description 休伯利安号的一行人来到了由逆熵镇守的前文明遗迹[海渊城],他们准备用巨大的传送装置[海渊之眼]进入量子之海,寻找丢失的渴望宝石.然而在行动前夜爱 ...