写在Android的主题应用写在、主题、Android

2023-09-04 05:42:05 作者:??.(故人)

我写这封信的Andr​​oid应用程序,并希望用户能够下载的主题和/或创造的人自己。该应用程序会来一些默认的图像,要么主题可以从市场安装/其他的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?

推荐答案

这种事情是不是真的支持作为标准配备的Andr​​oid,所以你走向了很多工作,给自己建造它。

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文件(因此加载其资源)。然后,您可以使用此背景下加载图像和这样的。请注意,这些资源都是从自己的应用程序的资源完全不同,所以你不能使用像主题或这样做替代标准的设施。你只需要手动将code无处不在,你要加载,从这个语境明确检索这是一个为主题的资源。

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.

要发现可用.apks(并由此包名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()找到所有与这些接收器.apks的:

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");