【问题标题】:Should an Android module or library be used to reuse fragments with different resources?是否应该使用 Android 模块或库来重用具有不同资源的片段?
【发布时间】:2015-11-01 06:50:28
【问题描述】:

我有一个想要在多个应用程序中使用的 Android 片段。该片段有许多用作按钮的图像。在每个应用程序中,我想使用一组不同的资源。例如,一个应用程序中的图像与另一个应用程序中的图像不同,但相同大小的图像之间仍然存在一对一的对应关系。

我的理解是模块是代码和资源,而普通库只是代码。如果我构建一个模块,是否所有与模块内部的片段相关的资源,或者使用该模块的不同应用程序的图像是否不同?假设片段请求的所有资源文件在应用程序中都可用,库方法可以工作吗?这似乎是非常糟糕的封装,但是如果模块中包含资源,我不知道如何避免这种情况。

【问题讨论】:

    标签: android android-fragments android-studio module android-library


    【解决方案1】:

    据我了解,您想使用库项目中的片段并希望更改特定于应用程序的图像。

    如果是这种情况,请将该库片段设为abstact,并强制子片段实现初始化图像视图的方法。这应该是这样的

    protected ImageView profilePicIV;

    // Abstract Methods
    protected abstract initialiseProfilePic();
    protected abstract initialisePic1();
    protected abstract initialisePic2();

    然后在库中扩展Fragment并实现方法。

    @override<br/> protected abstract initialiseProfilePic() { profilePicIV.setImageResource(R.drawable.xxx); }

    从库片段的构造函数中调用此方法。

    问候,

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 2022-11-26
      • 1970-01-01
      • 2015-12-11
      • 2017-01-06
      相关资源
      最近更新 更多