声明样式和样式之间的区别
我已经开始在我的 Android 应用程序中尝试样式等,到目前为止我已经让一切正常工作。我非常理解指南中的“风格”部分。
但是,环顾四周,如此线程中所示,我无法真正弄清楚两者之间的区别(declare-stylable
和 style
)。 根据我的理解,declare-styleable
采用其中指定的属性并将其指定为可样式,然后从代码中根据需要更改它。
但如果这就是它真正的作用,那么在布局中定义属性不是更简单吗?或者声明一个指定它的样式?
I've begun playing around with styles and such in my android applications, and I have gotten everything working so far. I quite understood the 'style' section of the guide.
But, looking around, as in this thread, I can't really figure out the difference between the two (declare-stylable
and style
).
From my understanding declare-styleable
takes the attribute specified in it and specifies it as styleable, and then from the code one changes it as he wants.
But if this is what it really does, wouldn't it be simpler to just define the attribute in the layout? Or declare a style specifying it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为将属性声明为可样式与不可样式之间只有以下区别。
在 attrs.xml 中,您可以直接在“resources”部分或“declare-styleable”中声明自定义属性:
因此,现在我们将“attrib1”定义为不可样式,将“attrib2”定义为可样式。
在
layout/someactivity.xml
中,我们可以直接使用这些属性(不需要命名空间):您可以在
style.xml
声明中使用“styleable”属性“attrib2”。同样,这里不需要命名空间(即使布局 XML 中使用了命名空间)。然后您还可以设置每个样式的属性。
假设我们这样做:我们直接在 XML 中设置
attrib1
,并在样式中设置attrib2
。在其他地方,我看到了一些描述,指出“blahblah”必须是使用这些属性的自定义视图类的名称,并且您需要使用命名空间来引用布局 XML 中的自定义属性。但这些似乎都没有必要。
可样式化和不可样式化之间的区别似乎是:
style.xml
”声明中使用可样式化属性。obtainStyledAttributes()
读取,非样式属性使用attr.getAttributeValue ()
或类似的。在我在网上看到的大多数教程和示例中,仅使用了
obtainStyledAttributes()
。但是,这不适用于直接在布局中声明而不使用样式的属性。如果您按照大多数教程中所示执行obtainStyledAttributes()
操作,则根本不会获得属性attrib1
;你只会得到attrib2
因为它是在样式中声明的。使用attr.getAttributeValue()
的直接方法有效:由于我们没有使用命名空间来声明“
attrib1
”,因此我们将null
作为命名空间参数传递在getAttributeValue()
中。I think there is only the following difference between declaring an attribute as styleable or not.
In attrs.xml you can declare custom attributes, either directly within the "resources" section or within "declare-styleable":
So now we defined "attrib1" as non-styleable and "attrib2" as styleable.
In
layout/someactivity.xml
we can use these attributes directly (no namespace needed):You can use the "styleable" attribute "attrib2" within a
style.xml
declaration. Again, no namespace is needed here (even if a namespace was used in the layout XML).Then you can also set the attributes per style.
Let us assume that we do this: we set
attrib1
directly in XML, and we setattrib2
in a style.Elsewhere I have seen descriptions stating that "
blahblah
" must be the name of the custom view class that uses these attributes, and that you need to use a namespace to refer to your custom attributes in the layout XML. But none of this seems to be necessary.The difference between styleable and non-styleable seems to be that:
style.xml
" declarations.obtainStyledAttributes()
, and the non-styled attributes withattr.getAttributeValue()
or similar.In most tutorials and examples I've seen on the Web, only the
obtainStyledAttributes()
was used. However, this does not work with attributes declared directly in layout, without using a style. If you doobtainStyledAttributes()
as shown in most tutorials, you will not get the attributeattrib1
at all; you will only getattrib2
since it was declared in the style. The direct method usingattr.getAttributeValue()
works:Since we used no namespace to declare "
attrib1
", we passnull
as the namespace argument ingetAttributeValue()
.查看此线程。
如果没有
declare-styleable
,就不可能创建新的自定义可绘制状态。Check this thread.
Without the
declare-styleable
it would not be possible to create a new custom drawable state.