首页 > 解决方案 > What is the difference b/w app:theme & android:theme in Android Programming

问题描述

I have seen this that some people write

app:theme="@style/xyz"

& on the other hand some write

android:theme="@style/xyz"

What is the difference b/w these 2 codes?

标签: androidandroid-studiocheckboxthemes

解决方案


app并且android实际上是命名空间。packages您可以像在 java 中一样考虑它们。android命名空间包含来自默认视图的属性,其中 asapp指的是来自应用程序命名空间的属性,例如来自自定义视图的属性。

因此,当您编写时android:theme,它将使用主题属性,该属性是默认视图的一部分,例如 TextView(它是 Android 的一部分)。现在,当您使用app:theme这意味着您正在谈论自定义主题属性而不是默认属性。当您构建扩展另一个视图的 CustomView 并且您还想更改theme属性分配值的方式时,这可能很有用,例如,您可能想要构建自定义视图以向 TextView 添加阴影并且您想要阴影主题中提供的颜色。

AppCompat 库中的视图app:theme用于制作自己的版本android:theme以支持旧版本。

TLDR;Usingandroid:theme使用 View 的默认主题功能,而app:theme用于自定义视图中的自定义主题功能。如果您使用的是 AppCompatView 或自定义视图,app:theme请在可用时使用,否则android:theme.

如果我在 MaterialDesign/AppCompat 或任何其他自定义视图中使用android:theme而不是会发生什么?app:theme

如果您这样做,所有视图中可用的视图属性将是可自定义的,但自定义视图添加的任何新属性都无法通过主题进行自定义。


推荐阅读