最近公司有需求需要在App里提供修改应用字体大小的功能,网上查了下资料,感觉都不是和理想,后决定自己实现功能。

实现原理

根据Activity的主题动态修改,在主题中添加自定义的字体大小属性,满足不同字体大小的需求,修改时将主题保存在SharedPreferences里面。当返回之前的Activity页面重新显示的时候,会调用onWindowFocusChanged方法,再根据当前的主题是否和SharedPreferences保存的主题是否相同决定是否recreate当前的Activity,当前的主题通过重写setTheme方法获取,废话不多说,直接上代码

package com.xc.theme;import android.app.Activity;
import android.content.SharedPreferences;
import android.support.annotation.StyleRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;/**** 所有需要跟随主题改变的类都继承此基类**/
public class BaseThemeActivity extends AppCompatActivity {/*** 保存当前使用的主题ID*/private int mCurrentThemeId;/*** 此方法会在onCreate方法之前被系统执行** @param resid*/@Overridepublic void setTheme(@StyleRes int resid) {int savedTheme = ThemeManager.getTheme(this);if (savedTheme > 0 && savedTheme != resid) {resid = savedTheme;}Log.e("print","setTheme before onCreate");mCurrentThemeId = resid;super.setTheme(resid);}@Overridepublic void onWindowFocusChanged(boolean hasFocus) {super.onWindowFocusChanged(hasFocus);if (hasFocus) {/**不依赖外部调用,通过系统回调函数当从设置页面返回就会自动调用*/ThemeManager.recreateIfThemeChanged(this, mCurrentThemeId);}}public static class ThemeManager {public static SharedPreferences getThemeSp(Activity context) {return context.getSharedPreferences("themes", MODE_PRIVATE);}public static int getTheme(Activity context) {return getThemeSp(context).getInt("savedTheme", -1);}/*** @param context    Activity对象* @param resid      主题的资源id* @param isRecreate 保存设置的主题后是否需要重新启动*/public static void setTheme(Activity context, @StyleRes final int resid, boolean isRecreate) {if (resid > 0) {getThemeSp(context).edit().putInt("savedTheme", resid).commit();if (isRecreate) {context.recreate();}}}public static void recreateIfThemeChanged(Activity context, int mCurrentThemeId) {int savedTheme = getTheme(context);if (savedTheme > 0 && savedTheme != mCurrentThemeId) {context.recreate();}}}
}

setTheme方法会在Activity的onCreate方法之前调用,用户系统设置当前的主题,我们重写此方法,判断传入的resid和我们保存的savedTheme是否相等,如果不相等,则覆盖resid设置我们保存的savedTheme,onWindowFocusChanged的作用是当从字体设置页面回到前一个页面的时候调用判断,这样就和Activity的回调函数关联,不用手动的调用修改页面主题,方法中判断是否和当前的主题相同,不相同的话重新启动Activity,调用recreate方法即可!

自定义字体的大小属性,style里的Theme里添加该属性

   <!--设置自定义字体属性--><declare-styleable name="ThemeTextSize"><attr name="smallSize" format="dimension" /><attr name="normalSize" format="dimension" /><attr name="largeSize" format="dimension" /></declare-styleable>

在主题中添加自定义的属性

<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item><item name="smallSize">14sp</item><item name="normalSize">16sp</item><item name="largeSize">18sp</item><item name="android:textColor">@android:color/holo_red_light</item></style><!-- Base application theme. --><style name="AppTheme.Smallsize" parent="AppTheme"><!-- Customize your theme here. --><item name="smallSize">12sp</item><item name="normalSize">14sp</item><item name="largeSize">16sp</item></style><!-- Base application theme. --><style name="AppTheme.NormalSize" parent="AppTheme"><!-- Customize your theme here. --><item name="smallSize">16sp</item><item name="normalSize">18sp</item><item name="largeSize">20sp</item></style><!-- Base application theme. --><style name="AppTheme.LargeSize" parent="AppTheme"><!-- Customize your theme here. --><item name="smallSize">20sp</item><item name="normalSize">22sp</item><item name="largeSize">24sp</item></style>

主页面布局编写

<?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:fitsSystemWindows="true"android:gravity="center_horizontal"android:orientation="vertical"tools:context=".MainActivity"><android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"android:theme="@style/AppTheme.AppBarOverlay"app:popupTheme="@style/AppTheme.PopupOverlay"app:title="首页" /><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="我是主页面"android:textColor="?android:textColor"android:textSize="?smallSize" /><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="我是主页面"android:textColor="?android:textColor"android:textSize="?normalSize" /><TextView
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="我是主页面"android:textColor="?android:textColor"android:textSize="?largeSize" /><Button
        android:id="@+id/btn_next"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="下一页" />
</LinearLayout>

字体大小通过
android:textSize=”?smallSize”
android:textSize=”?normalSize”
android:textSize=”?largeSize”
实现关联系统主题内的字体大小属性

最后上效果图

gif可能有点卡,真机上效果很好

注意:主题里面必须有对应的字体属性值才可以在布局xml内使用,不然会报错
下载地址

android仿QQ优雅的修改App字体大小相关推荐

  1. android launcher 字体大小,Android6.0 Launcher3 修改app字体大小

    在原生的Android6.0中,在修改了系统字体大小后,Launcher 3上的app字体大小没有改变,下面方法可以解决:--- a/packages/apps/Launcher3/src/com/a ...

  2. android架构师之路——修改app字体

    android自带的字体 通过 android:typeface="normal" 或者 android:fontFamily="monospace"两种方式来 ...

  3. android 仿qq修改头像,Qt:小项目仿QQ修改头像界面,技术点记录

    最近写了一个修改头像功能的UI,布局参考了QQ目前的修改头像界面.如下图 这里主要说明一下两个地方的技术:1.头像图片上层的遮罩层,圆形外部为灰色,内部为全透明:2.上传图片宽高比例可以通过鼠标拖拽移 ...

  4. android 设置ios 字体大小设置,解决修改系统字体大小APP字体跟着变大的问题

    前言 最近在做项目的时候,碰到了这个问题,具体是:app中字体大小用了sp,然后修改手机系统字体大小后,sp设置的文字大小跟着系统变了,导致了布局挤压以及一系列的问题,经过研究找到了解决方法,在此记录 ...

  5. android 设置字体大小不随系统大小变化,App字体大小不随系统改变而改变

    在 "设置" , "显示" , "字体大小" 里面我们可以设置系统字体大小 App界面字体,如果被修改之后,可能就达不到理想状态的效果,界面 ...

  6. android 仿QQ,微信群组里的@功能,支持@多人,并能一键删除,能获取上传对应的id(修改版)

    首先注明该文章是借签别人的博客,原文博文地址点击打开链接 android 仿QQ,微信群组里的@功能,支持@多人,并能一键删除,能获取上传对应的id 这个需求来源:本人做集成环信聊天时,项目需要@功能 ...

  7. Android仿QQ侧滑菜单

    先上效果图: GIF图有点模糊,源码已上传Github:Android仿QQ侧滑菜单 ####整体思路: 自定义ItemView的根布局(SwipeMenuLayout extends LinearL ...

  8. android qq分组展开,Android仿qq分组管理的第三方库

    本文实例为大家分享了Android仿qq分组管理的第三方库,供大家参考,具体内容如下 下面先看效果 我们点击展开与折叠分组的功能在库里面是已经封装好的,只能把它已入到项目中,就可以直接用了,十分的方便 ...

  9. Android禁止app字体大小跟随系统字体大小调节

    Android禁止app字体大小跟随系统字体大小调节 针对字体不随系统应用改变而改变,不然会导致原来的一些布局变化,在Application中重写getResources() @Overridepub ...

最新文章

  1. 论坛报名 | NLP 是否到了产业应用的黄金时代?
  2. 当深度学习遇上异构并行计算
  3. oracle集群图例
  4. Python: ImportRequestsError: No module named 'requests'解决方法
  5. 计算机职称业务工作业绩总结,档案职称工作业绩
  6. 在线分析mysql死锁详解_记一次线上mysql死锁分析(一)
  7. js 调用百度地图,并且定位用户地址,显示省市区街,经纬度
  8. Shopee2022届校园招聘提前批笔试
  9. Linux 查看网卡配置速率
  10. linux添加windows隐藏属性,解决文件夹隐藏属性无法取消的办法
  11. 睡眠 应该用 a加权 c加权_困成狗?谈谈睡眠研究的遗传发现之旅
  12. 搭建GitHub免费个人网站(详细教程)
  13. html iframe显示不全,滚动的iframe解决,但在iframe页面显示不全
  14. 怎么让搜狗收录-如何加快搜狗收录
  15. 递归实现树状分级部门树《部门单表》
  16. 通过微信公众号跳转H5页面领取现金红包
  17. 【TVM源码学习笔记】2.1 onnx算子转换
  18. iOS 在itunes connect创建App内购项目时遇到的坑
  19. oracle数据库可连接性检查
  20. 一份 Git cheat sheet 送给您

热门文章

  1. 计算机网路——163邮箱授权码
  2. 快速学习Java8新特性第七讲——Optional类
  3. es文件创建局域网服务器,大神来教你XBMC和ES文件浏览器局域网共享
  4. Bootstrap typeahead自动补全插件的坑
  5. #Geek Talk# AI and FinTech,投资阿里巴巴的 Benson Tam 也会跟大家一起 Talk
  6. 苹果IAP内购验证工具类
  7. 第十二天内容《基础交换十二》
  8. 沃尔什函数 与 沃尔什-哈达玛变换
  9. 二极管与门电路及原理分析
  10. 解决python利用openpyxl读取excel中公式结果值的问题