在开发中,一款APP会设置几款皮肤,也就会涉及到资源文件的引用问题。

现在来摸鱼记录一下吧。

一、首先建立几个style,也就是我们的皮肤style。比如:

<style name="ClassicTneme" parent="Theme.MaterialComponents.Light.NoActionBar"><item name="android:windowIsTranslucent">true</item><item name="colorPrimary">@color/primaryColor_theme1</item><item name="colorOnPrimary">#fafafa</item><item name="colorPrimaryDark">@color/primaryDarkColor_theme1</item><item name="colorPrimaryVariant">@color/primaryDarkColor_theme1</item><item name="colorSecondary">@color/secondaryColor_theme1</item><item name="colorSecondaryVariant">@color/secondaryLightColor_theme1</item><item name="colorOnSecondary">#fafafa</item><item name="android:textColor">#181818</item><item name="colorSurface">#ffffff</item><item name="colorOnSurface">#000000</item><item name="colorError">#ff0000</item><item name="actionModeBackground">@color/secondaryColor_theme1</item><item name="actionModeCloseDrawable">@null</item></style>
<style name="AppTheme2" parent="Theme.MaterialComponents.Light.NoActionBar"><item name="colorPrimary">@color/primaryColor_theme2</item><item name= "colorPrimaryDark">@color/primaryDarkColor_theme2</item><item name="colorPrimaryVariant">@color/primaryDarkColor_theme2</item><item name="colorSecondary">@color/secondaryColor_theme2</item><item name="colorSecondaryVariant">@color/secondaryLightColor_theme2</item><item name="colorOnPrimary">#ffffff</item><item name="colorOnSecondary">#fafafa</item><item name="android:textColor">#181818</item><item name="colorSurface">#ffffff</item><item name="colorOnSurface">#000000</item><item name="colorError">#ff0000</item><item name="actionModeBackground">@color/secondaryColor_theme2</item><item name="actionModeCloseDrawable">@null</item></style>

可以看到,这两款风格都有相同name的item,它们在不同的风格下的设置不同,当然对于图片资源也可以使用这种方式来配置,但是当图片资源过多的时候,每个风格下面都需要建立一个同名item,然后将这个item值分别设置为这两张图片,这样配置就很麻烦了,而且在新增资源文件的时候,需要在每个风格下配置,也很麻烦呢。

这种情况下,可以采用某种规律对图片进行命名,然后定义一套基于主题来搜索图片的方法就可以啦。比如,界面在两种风格下分别引用的是图片A,图片B。那么我们可以对图片A命名为icon_background_theme1,icon_background_theme2.这样我们在引用文件的时候,直接传入icon_background_theme1,根据当前设置的主题来对引用的资源名进行剪切拼接,从而获取我们想要的资源名称或者id。

这个时候就需要写一个这样的工具类啦。

public class ThemeUtil {private static String sThemeValue = "1";private static int sTheme = R.style.ClassicTneme;private static ContextThemeWrapper contextThemeWrapper;public static void initTheme(Application context) {LocalSharePara localSharePara = new LocalSharePara(context.getSharedPreferences("localShare", MODE_PRIVATE));sThemeValue = localSharePara.getThemeStyle();if (sThemeValue.equals("1")) {sTheme = R.style.ClassicTneme;} else if (sThemeValue.equals("2")) {sTheme = R.style.AppTheme2;} else {sTheme = R.style.AppTheme3;}contextThemeWrapper = new ContextThemeWrapper(context, sTheme);}public static int getTheme() {return sTheme;}public static int getAttrColor(@AttrRes int attr) {TypedValue typedValue = new TypedValue();contextThemeWrapper.getTheme().resolveAttribute(attr, typedValue, true);return typedValue.data;}@Nullablepublic static Drawable loadThemeDrawable(@DrawableRes int id) {Resources resources = Utils.getApp().getResources();String defaultName = resources.getResourceEntryName(id);return loadThemeDrawable(defaultName);}public static Bitmap loadThemeBitmap(@DrawableRes int id){Drawable drawable = loadThemeDrawable(id);Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565;Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),config);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicWidth());drawable.draw(canvas);return bitmap;}@SuppressLint("RestrictedApi")public static int loadThemeColor(@AttrRes int attrResId){return MaterialColors.getColor(contextThemeWrapper,attrResId, Color.BLACK);}@Nullablepublic static Drawable loadThemeDrawable(String defaultName) {String name = defaultName.replace("theme1", "theme" + sThemeValue);int id = ResourceUtils.getDrawableIdByName(name);if (id != 0) {return ResourceUtils.getDrawable(id);}return null;}public static int loadThemeDrawableId(@DrawableRes int defaultId) {Resources resources = Utils.getApp().getResources();String defaultName = resources.getResourceEntryName(defaultId);String name = defaultName.replace("theme1", "theme" + sThemeValue);int id = ResourceUtils.getDrawableIdByName(name);return id;}public static void applyImageViewTheme(ImageView imageView, @DrawableRes int id) {Drawable d = loadThemeDrawable(id);if (d != null) {imageView.setImageDrawable(d);}}
}

然后我们在Java代码中引用资源文件的时候,就通过这个工具类来引用,这不是整挺好么?

整挺好。

根据主题不同,引用不同的资源文件相关推荐

  1. 主module不能引用子module资源文件

    问题 当项目越做越大的时候我们往往会使用组件化开发,项目整体架构与下图类似: red的颜色虽然我们是在base模块中添加的,但是在App模块中依旧可以通过com.android.app.R来引用 但是 ...

  2. 文件属性及资源文件的使用

    C#文件属性 什么是文件属性 文件属性可以用来指示项目系统对应文件执行的操作.具体的操作见下文.Visual Basic和Visual C#的文件都具有4个属性:"FileName" ...

  3. 代码阅读总结之Fitch and Mather 7.0(资源文件Resource随笔)

    相关文章导航 Sql Server2005 Transact-SQL 新兵器学习总结之-总结 Flex,Fms3相关文章索引 FlexAir开源版-全球免费多人视频聊天室,免费网络远程多人视频会议系统 ...

  4. 如何在DNN模块中插入一个图片--在模块中引用资源文件

    DNN永远都是运行在网站的根目录 DNN的设计上最大的特点就是始终是使用一个网站根目录下的Default.aspx文件加载皮肤和模块从而实现整个网站的页面.这也就是说DNN这个Asp.net程序永远是 ...

  5. PyQt5 图形界面 - Qt Designer创建qrc资源文件引用图片资源实例演示,QTextBrower组件引用图片资源方法展示

    先看下效果图: 我们用 Text Brower 组件引用图片做个演示. 双击 Text Brower 组件进入编辑页面,右上角的图片就是打开资源管理器. 引用一个我们随便建的 qrc 结尾的文件. 信 ...

  6. android xml引用系统资源文件,android开发教程之系统资源的使用方法 android资源文件...

    一.颜色资源 1.颜色XML文件格式 //resources根元素 #000000 //color子元素 2.引用格式: java代码中:R.color.color_name  (这是一个int型的数 ...

  7. Eclipse 下如何引用另一个项目的资源文件

    为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...

  8. SilverLight学习笔记--关于Silverlight资源文件(如:图片)的放置位置及其引用

    Silverlight中有许多资源文件,例如:图片,音频.视频,甚至XML和XAML等非执行数据文件,在Silverlight中根据资源所处位置的不同而有所区别.    资源文件可分布在我们Silve ...

  9. WPF引用外部类库中的资源文件提示不能找到的解决方法

    WPF引用外部类库中的资源文件提示不能找到的解决方法 参考文章: (1)WPF引用外部类库中的资源文件提示不能找到的解决方法 (2)https://www.cnblogs.com/scy251147/ ...

  10. 火狐浏览器不能访问ftp服务器的文件夹,火狐浏览器计划后续版本禁用网页引用FTP子资源...

    火狐浏览器项目组目前已经更新计划决定在六月份到来的61版里禁用网页引用通过FTP协议传输的资源文件. 禁用的原因同样是基于安全考虑:由于FTP协议已经不再安全因此若是被劫持则可能会被用来分发恶意软件. ...

最新文章

  1. Realm发布Realm .NET,扩展支持.NET技术栈
  2. CCNA和四级网工的对比
  3. C++11学习之share_ptr和weak_ptr
  4. 关于delphi中的register, pascal, cdecl, stdcall, safecall
  5. 日志、下载、投影、连接查询
  6. 约三分之二的 DDoS 攻击指向通信服务提供商
  7. Ui5 tool debug - ctrl alt shift s
  8. 数据库设计(四)概念数据模型
  9. 闲鱼直播三周内实现点击率翻倍,我们是这么做到的...
  10. 卢伟冰:Redmi K30会支持全网通5G 雷军:必须的!
  11. 打印Fibonacci数列方法汇总(前20项,每行5个)
  12. PacMan开发-碰撞检测实现
  13. jQuery中的$(“this“)和$(this)的区别
  14. 红胖子网络科技博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中...
  15. SPSS的下载和使用经历
  16. 富士通Fujitsu DPK9500GA Pro 打印机驱动
  17. 2022-2027年中国金融科技行业市场调研及未来发展趋势预测报告
  18. 经典配色方案之 红、橙、黄、绿、青、紫、无彩色系
  19. 由磁场数据和加速度数据计算初始姿态角
  20. 计算机专业的书普遍都这么贵,你们都是怎么获取资源的?---给你们推荐一些编程电子书籍下载网站

热门文章

  1. 软件设计师教程第5版.PDF.高清
  2. Java渐变进度条_很好看的java进度条
  3. Protel99SE精彩教程
  4. 51单片机学习——1天学完普中基本实验例程,走马观花式学习,大家切勿效仿。
  5. X5 浏览器内核调研报告
  6. android浏览器病毒,2018安卓手机杀毒软件排行榜
  7. SQLServer 2016安装时的错误:Polybase要求安装Oracle JRE 7更新51或更高版本
  8. Python SPSS教程
  9. 汉字字符编码在线查询的网站
  10. Linux中vi命令用法