高仿Android 点心桌面皮肤实现方式
不错的帖子:
android之ViewFlipper实现左右滑动动画效果
http://www.eoeandroid.com/thread-235446-1-1.html
Android TreeView 树形结构
http://www.eoeandroid.com/thread-235033-1-1.html
前段时间有空研究了一下Launcher,做了一个自己的桌面,像点心桌面那样可以切换皮肤的,所以在这里和大家分享一下应用皮肤的实现方式。我是这样实现的,主的应用是一个apk,皮肤是一个apk,在主的应用中读取已安装的apk。
先附上几张切图
先来讲解一下皮肤apk吧,下面是皮肤apk的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.GreenSkinExample" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".GreenSkin" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="com.app.skinexample.Theme"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> </manifest>
其中<action android:name="com.app.skinexample.Theme"/>是比较重要的,我们就靠它在主应用中找到此皮肤。然后皮肤apk的res文件夹里放需要的图片、文字、颜色资源。
在主应用apk中,我们用以下代码片段来读取皮肤apk
public static final String THEME_ACTION = "com.app.skinexample.Theme" ; public List<ResolveInfo> themes ; public void GetAllThemes() { Intent mIntent = new Intent(THEME_ACTION) ; PackageManager pm = getPackageManager() ; themes = pm.queryIntentActivities(mIntent, 0) ; }
最重要的,如何在主apk中使用皮肤apk替换原有的呢,看一下代码
public Resources themeResources = null ; public String themePackage ; public void ThemeLoading() { PackageManager pm = getPackageManager() ; try { themeResources = pm.getResourcesForApplication(themePackage) ; } catch (NameNotFoundException e) { //The saved theme was uninstalled so we save the default one } Drawable icon = null ; int resource_id=themeResources.getIdentifier (resourceName, "drawable", themePackage); if(resource_id!=0){ icon=themeResources.getDrawable(resource_id); } }
仿点心桌面皮肤实现方式程序源码:
ThemeUtil.java
public static void DisplayToast(Context context,String text) { Toast toast ; toast = Toast.makeText(context , text, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } public static void loadThemeResource(Resources themeResources,String themePackage, String item_name, View item,int themeType) { Drawable d=null; if(themeResources!=null) { int resource_id=themeResources.getIdentifier (item_name, "drawable", themePackage); if(resource_id!=0) { d=themeResources.getDrawable(resource_id); if(themeType==ThemeAttributes.THEME_ITEM_FOREGROUND && item instanceof ImageView) { Drawable tmp=((ImageView)item).getDrawable(); if(tmp!=null){ tmp.setCallback(null); tmp=null; } ((ImageView)item).setImageDrawable(d); } else { Drawable tmp=item.getBackground(); if(tmp!=null){ tmp.setCallback(null); tmp=null; } item.setBackgroundDrawable(d); } } } } public static Drawable loadThemeDrawable(Resources themeResources , String resourceName , String themePackage) { Drawable icon = null ; int resource_id=themeResources.getIdentifier (resourceName, "drawable", themePackage); if(resource_id!=0){ icon=themeResources.getDrawable(resource_id); } return icon ; } public static int loadThemeColor(Resources themeResources , String resourceName , String themePackage) { int color = 0 ; int resource_id=themeResources.getIdentifier (resourceName, "color", themePackage); if(resource_id!=0){ color=themeResources.getColor(resource_id); } return color ; }
首发地址:http://www.eoeandroid.com/thread-231199-1-1.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构