如何在 Android XML 资源中引用在库中定义的颜色?

huangapple go评论57阅读模式
英文:

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 :

&lt;resources&gt; &lt;color name=&quot;bg_line&quot;&gt;#E7E7E7&lt;/color&gt; ...

fragment_main.xml :

&lt;RelativeLayout android:background=&quot;@color/bg_line&quot; ... &gt;...

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 :

&lt;manifest package=&quot;de.k3b.mylib&quot; ...&gt;

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

huangapple
  • 本文由 发表于 2023年3月7日 19:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定