直播平台源码,Android 中的字体大小适配
直播平台源码,Android 中的字体大小适配
小号字体下,会比较精致,不作处理。对于大号字体,使其fontScale强制改为指定值,以解决布局错乱的情况。
封装为工具类如下
1 | <br> public class FontCompatUtils {<br> public static final float MAX_FONT_SCALE = 1.10F; //可自行修改最大缩放值<br> public static final String TAG = "FontCompatUtils";<br> private static Float fontScalePercent = null;<br> public static Resources getResources(Resources res) {<br> Configuration configuration = res.getConfiguration();<br> if (fontScalePercent == null) {<br> fontScalePercent = 1 / configuration.fontScale;<br> }<br> if (shouldChangeFontScale(configuration)) {//非默认值<br> Configuration newConfig = new Configuration();<br> newConfig.setToDefaults();//设置默认<br> //configuration.fontScale = MAX_FONT_SCALE;<br> res.updateConfiguration(newConfig, res.getDisplayMetrics());<br> }<br> return res;<br> }<br> /**<br> * 是否需要改变字体缩放级别<br> *<br> * @param configuration<br> * @return<br> */<br> public static boolean shouldChangeFontScale(Configuration configuration) {<br> return configuration.fontScale > MAX_FONT_SCALE;<br> }<br> /**<br> * 字体缩放比例<br> *<br> * @return<br> */<br> public static Float getFontScalePercent() {<br> if (fontScalePercent == null) {<br> return 1F;<br> }<br> return fontScalePercent;<br> }<br>} |
然后,在Application和BaseActivity中,重写getResources和onConfigurationChanged方法
1 | <br>@Override<br> public void onConfigurationChanged(Configuration newConfig) {<br> if (FontCompatUtils.shouldChangeFontScale(newConfig))<br> getResources();<br> super.onConfigurationChanged(newConfig);<br>}<br>@Override<br> public Resources getResources() {<br> return FontCompatUtils.getResources(super.getResources());<br>} |
以上就是直播平台源码,Android 中的字体大小适配, 更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现