声明样式和样式之间的区别

发布于 2024-10-10 07:04:31 字数 403 浏览 0 评论 0原文

我已经开始在我的 Android 应用程序中尝试样式等,到目前为止我已经让一切正常工作。我非常理解指南中的“风格”部分。

但是,环顾四周,如此线程中所示,我无法真正弄清楚两者之间的区别(declare-stylablestyle)。 根据我的理解,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-10-17 07:04:31

我认为将属性声明为可样式与不可样式之间只有以下区别。

在 attrs.xml 中,您可以直接在“resources”部分或“declare-styleable”中声明自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <attr name="attrib1" format="string" />
 <declare-styleable name="blahblah">
    <attr name="attrib2" format="string" />
 </declare-styleable>

因此,现在我们将“attrib1”定义为不可样式,将“attrib2”定义为可样式。

layout/someactivity.xml 中,我们可以直接使用这些属性(不需要命名空间):

<com.custom.ViewClass  attrib1="xyz" attrib2="abc"/>

您可以在 style.xml 声明中使用“styleable”属性“attrib2”。同样,这里不需要命名空间(即使布局 XML 中使用了命名空间)。

 <style name="customstyle" parent="@android:style/Widget.TextView">
    <item name="attrib2">text value</item>
    <!--  customize other, standard attributes too: -->
    <item name="android:textColor">@color/white</item>
 </style>

然后您还可以设置每个样式的属性。

<com.custom.CustomView attrib1="xyz" style="@style/customstyle"/>

假设我们这样做:我们直接在 XML 中设置 attrib1,并在样式中设置 attrib2

在其他地方,我看到了一些描述,指出“blahblah”必须是使用这些属性的自定义视图类的名称,并且您需要使用命名空间来引用布局 XML 中的自定义属性。但这些似乎都没有必要。

可样式化和不可样式化之间的区别似乎是:

  • 您可以在“style.xml”声明中使用可样式化属性。
  • 自定义类的构造函数需要以不同的方式读取样式属性和非样式属性:样式属性使用 obtainStyledAttributes() 读取,非样式属性使用 attr.getAttributeValue () 或类似的。

在我在网上看到的大多数教程和示例中,仅使用了 obtainStyledAttributes()。但是,这不适用于直接在布局中声明而不使用样式的属性。如果您按照大多数教程中所示执行 obtainStyledAttributes() 操作,则根本不会获得属性 attrib1;你只会得到 attrib2 因为它是在样式中声明的。使用 attr.getAttributeValue() 的直接方法有效:

 public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    String attrib1 = attrs.getAttributeValue(null, "attrib1");
    // do something with this value
 }

由于我们没有使用命名空间来声明“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":

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <attr name="attrib1" format="string" />
 <declare-styleable name="blahblah">
    <attr name="attrib2" format="string" />
 </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):

<com.custom.ViewClass  attrib1="xyz" attrib2="abc"/>

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

 <style name="customstyle" parent="@android:style/Widget.TextView">
    <item name="attrib2">text value</item>
    <!--  customize other, standard attributes too: -->
    <item name="android:textColor">@color/white</item>
 </style>

Then you can also set the attributes per style.

<com.custom.CustomView attrib1="xyz" style="@style/customstyle"/>

Let us assume that we do this: we set attrib1 directly in XML, and we set attrib2 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:

  • You can use styleable attributes in "style.xml" declarations.
  • The constructor of the custom class needs to read the styled and the non-styled attributes in a different way: the styled attributes with obtainStyledAttributes(), and the non-styled attributes with attr.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 do obtainStyledAttributes() as shown in most tutorials, you will not get the attribute attrib1 at all; you will only get attrib2 since it was declared in the style. The direct method using attr.getAttributeValue() works:

 public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    String attrib1 = attrs.getAttributeValue(null, "attrib1");
    // do something with this value
 }

Since we used no namespace to declare "attrib1", we pass null as the namespace argument in getAttributeValue().

暮色兮凉城 2024-10-17 07:04:31

查看此线程

如果没有declare-styleable,就不可能创建新的自定义可绘制状态。

Check this thread.

Without the declare-styleable it would not be possible to create a new custom drawable state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文