java - 跨 Android 应用程序使用的自定义 View 和属性

标签 java android svn android-custom-view

我现在已经开发了几个 Android 应用程序,并创建了一个我经常在多个应用程序中使用的类的代码库。此代码全部在一个颠覆 (svn) 存储库中,每个应用程序都在其自己的存储库中。然后每个应用程序都有所需包的 svn:externals 引用(例如,com.company.android.views)。这非常有效,除非必须为自定义属性导入 R 类。

自定义 View 有这样的导入:

import com.company.apps.myapp.R

这样它就可以有这样的代码:

attributes.getBoolean(R.styleable.WebImageView_autoload, autoload)

该自定义属性在 res/values/attrs.xml 中定义:

<declare-styleable name="WebImageView">
    <attr name="autoload" format="boolean" />
    ...others
</declare-styleable>

这非常有效,但是当我有第二个使用相同 View 的应用程序时,问题就来了。现在,我将导入更新为 import com.company.apps.anotherapp.R,这样它就可以与“anotherapp”一起工作,并用“myapp”打破它。当同时处理多个应用程序时,这会成为一个问题。

我的临时解决方案是 checkin 适用类的更新,然后将 svn:externals 锁定到该特定修订版。每个应用程序最终都被锁定到不同的修订版,这很快就会变得困惑,但这似乎仍然比直接将各种类复制到应用程序的存储库中要好。

我想到的唯一其他解决方案是使用反射,例如:

Class class = Class.forName(context.getPackageName() + ".R");
Field[] fields = class.getDeclaredFields();

然后遍历字段,将我关心的字段分配给整个类中使用的变量。不过,这看起来相当笨拙,尤其是当我们可能讨论需要执行此操作的多个类时。

我该如何解决这个问题?有没有办法动态导入 com.company.apps.*.R 或以某种方式生成不依赖于特定应用程序的不同 R 类?还是我完全错过了其他一些明显(或不太明显)的解决方案?

最佳答案

花了我一段时间,但我找到了一个很好的答案:Library Projects .

Structurally, a library project is similar to a standard Android application project. For example, it includes a manifest file at the project root, as well as src/, res/ and similar directories. The project can contain the same types of source code and resources as a standard Android project, stored in the same way. For example, source code in the library project can access its own resources through its R class.

关于java - 跨 Android 应用程序使用的自定义 View 和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655014/

相关文章:

java - 使用 Spring Data Pageable 的 JPA Criteria 查询总和

android - setCompoundDrawablesWithIntrinsicBounds 无法正常工作

java - 火力地堡 : updatePhoneNumber for Current User who logged in with google

android - Calabash 使用 Paypal 进行自动化测试

java - @ComponentScan 在 Spring Boot AutoConfiguration 类中不起作用?

java - 无法使用 POI 3.11 beta1 从数据透视表读取数据

java - Spring-kafka 偏移提交方式

svn - 创建一个 svn 存储库作为 https ://servername/svn in Windows?

svn - 为什么我在访问 VisualSVN Server 时在 svn 客户端 "XML parse error at line 1 no element found"上收到错误?

svn - 我需要将 SVN 日志历史记录导出到 Windows 中的 xml 文件中