英文:
How to reference a color in a android xml resource that is defined in a lib?
问题
I have an android fragment app/src/main/res/layout/fragment_main.xml
that references a color bg_line
defined in app/src/main/res/values/colors.xml
res/values/colors.xml
:
<resources>
<color name="bg_line">#E7E7E7</color>
...
</resources>
fragment_main.xml
:
<RelativeLayout android:background="@color/bg_line" ... >...
I want to refactor my existing working code and move color bg_line
into a different android library module mylib/src/main/res/values/colors.xml
that uses
mylib/src/main/manifest.xml
:
<manifest package="de.k3b.mylib" ...>
My Question: how can i reference the color bg_line
in fragment_main.xml
if bg_line
is defined in module mylib
In https://developer.android.com/guide/topics/resources/color-list-resource i found
> resource reference:
> In Java: R.color.filename
> In XML: @[package:]color/filename
I found no example use for [package:]
i tried
android:background="@android:color/bg_line"
android:background="@app:color/bg_line"
android:background="@mylib:color/bg_line"
android:background="@de.k3b.mylib:color/bg_line"
I am using Android Studio (Electric Eel | 2022.1.1 Patch 2)
英文:
I have an android fragment app/src/main/res/layout/fragment_main.xml
that references a color bg_line
defined in app/src/main/res/values/colors.xml
res/values/colors.xml
:
<resources> <color name="bg_line">#E7E7E7</color> ...
fragment_main.xml
:
<RelativeLayout android:background="@color/bg_line" ... >...
I want to refactor my existing working code and move color bg_line
into a different android library module
mylib/src/main/res/values/colors.xml
that uses
mylib/src/main/manifest.xml
:
<manifest package="de.k3b.mylib" ...>
My Question: how can i reference the color bg_line
in fragment_main.xml
if bg_line
is defined in modul :mylib
in https://developer.android.com/guide/topics/resources/color-list-resource i found
<pre>
> resource reference:
> In Java: R.color.filename
> In XML: @[package:]color/filename
</pre>
- I found no example use for [package:]
i tried
- android:background="@android:color/bg_line"
- android:background="@app:color/bg_line"
- android:background="@mylib:color/bg_line"
- android:background="@de.k3b.mylib:color/bg_line"
I am using Android Studio (Electric Eel | 2022.1.1 Patch 2)
答案1
得分: 1
包含布局片段 main.xml 的模块必须实现(Gradle)包含颜色定义的模块(:mylib)。因此,它只是将颜色引用为 "before"(android:background="@color/bg_line")。
英文:
the module that contains the layout fragment main.xml must implement (gradle) the module (:mylib) where the color definition is found. So it's just referencing the color as "before" (android:background="@color/bg_line").
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论