在 Android 中编写主题应用程序 [英] Writing themed applications in Android

查看:28
本文介绍了在 Android 中编写主题应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Android 编写一个应用程序,并希望用户能够下载主题和/或自己创建主题.该应用会附带一些默认图像,主题可以从 Market/作为其他 APK 安装,也可以放在 SD 卡上 - 都可以.

I am writing an application in Android and want users to be able to download themes and/or create ones themselves. The app would come with some default images and either themes could be installed from Market/as other APKs or could be put on sdcard - either would be fine.

我想要的是:- 允许主题覆盖按钮中的图像 - 现在我有 XML 样式来执行选择时突出显示等操作(用于按钮背景 + 用于将按钮 bg 与实际按钮图像组合)- 允许主题覆盖某些内容的背景和文本颜色(我现在不使用那里的样式,但我可以,如果这能让事情变得更容易)

What I want is: - allow themes to override images in buttons - right now I have XML styles to do things like highlighting when selected ( for button background + for combining button bg with actual button images) - allow themes to override background and text colors for certain things (I'm not using styles there for now, but I could if that would make things easier)

我想知道是否可以根据配置在我的应用程序/活动的 onCreate 中覆盖样式定义 - 我不介意用户在样式更改后必须重新启动应用程序.

I am wondering if it's simply possible to override style definitions in my Application/Activity's onCreate based on configuration - I wouldn't mind user having to restart the app after style change.

另外,如果它是另一个 APK,我的主 APK 将如何获取它 - 通过枚举包并按包名称过滤?

Also, if it would be another APK, how would my main APK get it - by enumerating packages and filtering by package name?

是否有关于它的好教程?

Is there perhaps a good tutorial about it?

推荐答案

Android 中并没有真正支持这种东西作为标准工具,因此您需要进行大量工作来自己构建它.

This kind of thing isn't really supported as a standard facility in Android, so you are heading towards a lot of work to construct it yourself.

您需要了解的第一件事是 Context.createPackageContext(),它允许您为另一个 .apk 创建一个 Context(从而加载其资源).然后您可以使用该上下文来加载图像等.请注意,这些资源与您自己的应用程序资源完全不同,因此您不能使用主题等标准工具进行替换.您只需要手动将代码放置在您想要加载主题"资源的任何位置,该资源从该上下文中显式检索它.

The first thing you need to know about is Context.createPackageContext(), which allows you to create a Context for another .apk (and thus load its resources). You can then use that Context to load the images and such. Note that these resources are completely distinct from your own app's resources, so you can't use standard facilities like themes or such to do the replacement. You will just need to manually put code everywhere you want to load a "themed" resources that explicitly retrieves it from this Context.

要发现可用的 .apk(以及与 createPackageContext() 一起使用的包名称),您可以使用 PackageManager 工具来查询组件.例如,你可以说每个主题 .apk 都会有一个这样的:

To discover the available .apks (and thus the package name to use with createPackageContext()), you can use the PackageManager facilities to do a query for components. For example, you can say that each theme .apk will have a like this:

<receiver android:name="something">
    <intent-filter>
        <action android:name="com.mydoman.action.THEME" />
    </intent-filter>
</receiver>

现在您可以通过 PackageManager.queryBroadcastReceivers() 找到所有带有此类接收器的 .apk:

Now you can find all of the .apks with such receivers through PackageManager.queryBroadcastReceivers():

http://developer.android.com/reference/android/content/pm/PackageManager.html#queryBroadcastReceivers(android.content.Intent, int)

要选择具有上述意图过滤器的对象,请使用以下内容制作的意图:

To select those with the above intent filters, use a Intent made with:

Intent intent = new Intent("com.mydoman.action.THEME");

这篇关于在 Android 中编写主题应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆