【问题标题】:Reference resources from other module in Android Studio从 Android Studio 中的其他模块引用资源
【发布时间】:2018-01-30 15:09:32
【问题描述】:

我有一个主应用模块以及一些库模块。

我的库模块如何从主应用模块引用资源。

例如: 我的基础应用程序有一个 colors.xml,其中有一个名为“AppPrimary”的颜色项,它是蓝色的。

在我的库中,我希望 xml 布局中的视图引用 @color/AppPrimary 颜色,但这不起作用。

我怎样才能做到这一点?

编辑:我应该指定这不是专门的 appCompat 资源。只是我的主模块中的通用字符串/颜色/样式/。如何在我的库模块 colors.xml/strings.xml 的同一个项目中引用它们?

【问题讨论】:

    标签: android android-studio android-resources


    【解决方案1】:

    在您的库模块中的 xml 布局引用 ?attr/colorPrimary 而不是 @color/AppPrimary

    这引用了应用主题中的调色板属性。 IE。;

    <item name="colorPrimary">@color/AppPrimary</item>
    

    您还可以参考其他调色板属性,如:

    <item name="colorPrimaryDark">...</item> -> ?attr/colorPrimaryDark
    <item name="colorAccent">...</item> -> ?attr/colorAccent
    

    等等。

    Please refer to this documentation

    警告
    这些示例引用了主题中的 AppCompat 实现。即没有 android: 命名空间前缀。

    -- 编辑--

    简短的回答是,您根本无法从引用库 colors.xml 文件的主模块中“获取”定义的颜色。

    但是,您可以在主题中利用自定义属性来实现相同的效果。

    这可以通过在库模块的attrs.xml 文件中声明一个通用样式来实现,如下所示:

    <declare-styleable name="BaseTheme">
        <attr name="someColor" format="reference|color" />
    </declare-styleable>
    

    注意:样式的名称与此实现无关,因此请随意命名。

    然后在“style.xml”或“theme.xml”文件中的主模块主题定义中,您需要将此属性设置为您要共享的颜色,如下所示:

    <style name="Theme.Example" parent="Theme.AppCompat.Light.NoActionBar">
    
        <!-- Color Palette -->
        <item name="colorPrimary">...</item>
        <item name="colorPrimaryDark">...</item>
        <item name="colorAccent">...</item>
    
        <!-- Here we define our custom attributes -->
        <item name="someColor">@color/dark_blue</item>
    
    </style>
    

    定义后,您可以在库模块 XML 代码中引用该属性。例如;

    <TextView
        ...
        android:textColor="?attr/someColor"
        />
    

    【讨论】:

    • 我应该指定这些不是 appcompat 资源。说它只是在名为“dark_blue”的主应用程序模块中定义的一般颜色。如何在我的库 colors.xml 中获得深蓝色?
    • 我已经更新了答案以更好地适应您的用例。
    • 感谢 r0adkl 我已经更改了项目结构。相反,我定义了一个通用核心库,所有模块(库和基础应用程序)都可以使用应用程序颜色/通用字符串等引用它。
    • 如果外部模块具有可执行的“东西”,它可以工作,但如果我使用外部模块作为可绘制对象的容器呢?
    【解决方案2】:

    以下是在库模块中以编程方式获取颜色属性的方法。

    @ColorInt
    public static int getAttributeColor(Context context, @AttrRes int colorAttribute)
    {
        int[] attrs = {colorAttribute};
        TypedArray ta = context.obtainStyledAttributes(attrs);
        /*Get the color resourceID that we want (the first index, and only item, in the
        attrs array). Use ContextCompat to get the color according to the theme.
        */
        @ColorInt int color = ContextCompat.getColor(context,
                                                     ta.getResourceId(0, -1));
        // ALWAYS call recycle() on the TypedArray when you’re done.
        ta.recycle();
        return color;
    }
    

    例如

    //Get the main applications ‘colorAccent' from the app’s color palette.
    int appAccentColor = Utility.getMarketAttributeColor(context, R.attr.colorAccent);
    
    //Get the color attribute your module made for the client app to define.
    int moduleAttrColor = Utility.getMarketAttributeColor(context, R.attr.moduleColorAttr);
    

    注意:此基本属性检索过程适用于大多数属性类型。

    【讨论】:

      猜你喜欢
      • 2020-07-09
      • 2011-03-18
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多