b/w app:theme & 有什么区别?android:Android 编程中的主题 [英] What is the difference b/w app:theme & android:theme in Android Programming

查看:34
本文介绍了b/w app:theme & 有什么区别?android:Android 编程中的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些人写的这个

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?

推荐答案

appandroid 实际上是命名空间.你可以像 java 中的 packages 一样思考它们.android 命名空间包含来自默认视图的属性,而 app 指的是来自您的应用命名空间的属性,例如来自自定义视图的属性.

app and android are actually namespaces. You can think of them in the same way as packages in java. android namespace contains attributes from default views where as app refers to attributes from your app's namespace such as from custom views.

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

So when you write android:theme, it will use theme attribute which is part of default view such as a TextView(which is part of Android). Now when you use app:theme this means you are talking about a custom theme attribute and not the default one. This can be useful when you are building a CustomView that extends another view and you also want to change how the theme attribute assigns values, For example, you might you want to build a custom view to add a shadow to a TextView and you want the shadow color to be provided in the theme.

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

Views from AppCompat library use app:theme to make their own version of android:theme to support older versions.

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

TLDR; Using android:theme uses View's default theme function whereas app:theme is for a custom theme function in custom views. If you are using an AppCompatView or a custom view, use app:theme when available, otherwise android:theme.

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

What happens if I use android:theme instead of app:theme in MaterialDesign/AppCompat or any other custom view?

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

If you do that, view's attributes that are available in all views will be customizable but any new attribute added by the custom view won't be customizable through theme.

这篇关于b/w app:theme & 有什么区别?android:Android 编程中的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆