如何通过 R.attr.colorPrimary 访问主题颜色属性

问题描述 投票:0回答:1

我正在使用 Android Studio Electric Eel,我开始了一个新的“基本活动”项目。它带有明暗主题,/res/values/theme.xml /res/values-night/theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.Example" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/md_theme_light_primary</item>
    </style>
</resources>

我明白我需要使用这个代码:

val typedValue = TypedValue()
view.context.theme.resolveAttribute(R.attr.colorPrimary, typedValue, true)
val colorPrimary = typedValue.data

这几乎行得通。 无论出于何种原因,R.attr.colorPrimary

 都不会解决我本地项目的默认 
com.example.project.R
。当我输入
R.attr.colorPrimary
时,它只会自动完成解析为
com.google.android.material.R.attr.colorPrimary
(它也可以解析为一些androidx组件,这也有效)

当我使用它时,它确实有效,并且它确实使用了从我的 theme.xml 指向的我的 colors.xml。我只是想了解为什么它不会简单地从我的 theme.xml 中解析。在我在网上找到的所有示例中,它都被命名为 styles.xml,所以我将其重命名为那个,一切都一样,没有变化。

当我按住 ctrl 并单击已解析的颜色时,它会将我带到缓存文件中... ransformed ppcompat-1.6.1 es alues alues.xml 那是

<attr format="color" name="colorPrimary"/>


如果我 ctrl-click

parent="Theme.MaterialComponents.DayNight.DarkActionBar"

 它会带我到一个缓存 ... ransformed\material-1.8.0
es 值 alues.xml

我只是想明白为什么。看来我的颜色仍然覆盖默认颜色,并且它可以使用

com.google.android.material.R

。在 xml 布局中,
?attr/colorPrimary
 工作得很好。但是我看的任何地方都没有说在以编程方式解决它时需要这条路径。

android android-studio material-design android-resources android-theme
1个回答
0
投票
这几乎行得通。无论出于何种原因,R.attr.colorPrimary 都不会解析我本地项目的默认 com.example.project.R。

那是因为

colorPrimary

 不是您在本地项目中定义的属性。这是材料库中定义的属性。

当我使用它时,它确实有效,并且它确实使用了从我的 theme.xml 指向的我的 colors.xml。我只是想了解为什么它不会简单地从我的 theme.xml 中解析。

属性不会“从 [your] theme.xml 解析”。属性在

attrs.xml

 文件中定义,就像颜色或尺寸一样。你没有定义一个,因此它不存在。

当你在你的

themes.xml

文件中使用它时:

<resources xmlns:tools="http://schemas.android.com/tools"> <style name="Theme.Example" parent="Theme.MaterialComponents.DayNight.DarkActionBar"> <item name="colorPrimary">@color/md_theme_light_primary</item> </style> </resources>
您正在 

extending 现有的材料主题(parent

 属性)并设置已在该主题中定义的 
colorPrimary
 属性。您没有声明自己的 
colorPrimary
 属性。如果您没有将 
parent
 属性设置为具有 
colorPrimary
 的主题,则会出现错误。

在我在网上找到的所有示例中,它都被命名为 styles.xml,所以我将其重命名为那个,一切都一样,没有变化。

对 - 文件名无关紧要。

当我按住 ctrl 并单击已解析的颜色时,它会将我带到缓存文件中... ransformed ppcompat-1.6.1 es alues alues.xml 那是

如果我 ctrl-click the parent="Theme.MaterialComponents.DayNight.DarkActionBar" 它会带我到缓存 ... ransformed\material-1.8.0 es 值 alues.xml

通过 XML 的 Android 主题是一个复杂的嵌套覆盖网络。

colorPrimary

 作为第一个“app compat”库中的 Material design 引入的概念。此后,它通过专用材料库进行了更新和增强,这些材料库扩展了原始材料并重新定义了值以遵守最新的材料指南。

我只是想明白为什么。看来我的颜色仍然会覆盖默认颜色,并且它可以使用 com.google.android.material.R 工作。在 xml 布局中 ?attr/colorPrimary 工作得很好。但是我看的任何地方都没有说在以编程方式解决它时需要这条路径。

总而言之,

colorPrimary

 是由 appcompat 和材料设计库定义的主题属性,因此它存在于它们的包中。

在 XML 中,

?attr/colorPrimary

 引用当前主题,在您的情况下将是“Theme.Example”,您已声明将 
colorPrimary
 设置为 
@color/md_theme_light_primary
.

但是,在代码中,

colorPrimary

 只是一个常量,必须解决以澄清 
which colorPrimary
 您所指的是所有已定义的内容。最终,您使用哪个包来访问代码中的
colorPrimary
并不重要 - 它的
value将解析为您在主题中设置的内容。

© www.soinside.com 2019 - 2024. All rights reserved.