通常,自定义属性的所有示例均采用以下形式:
<declare-stylable name="MyView">
<attr name="name" format="string"/>
</declare-styleable>
及其用法:
<com.example.test.MyView
customns:name="Hello"/>
因此,自定义视图的名称与可样式化属性的名称相同。
但是in this example (click for full code)您会看到:
<declare-styleable name="Options">
<attr name="titleText" format="string" localization="suggested" />
<attr name="valueColor" format="color" />
</declare-styleable>
用于:
<com.vogella.android.view.compoundview.ColorOptionsView
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
custom:titleText="Background color"
custom:valueColor="@android:color/holo_green_light"
/>
我想知道,
ColorOptionsView
如何链接到以名称Options
定义的属性? 最佳答案
这些选项可作为声明的namespace
自定义的一部分使用,该自定义包含在XML
文件的顶部:
xmlns:custom="http://schemas.android.com/apk/res/com.vogella.android.view.compoundview"
注意
仅添加此行将不提供对自动完成的支持。如果这是您的问题的意思,则需要将模式添加到工作区的XML目录中。您可以在Eclipse中通过依次转到
Eclipse -> Preferences
和XML -> XML Catalog
来执行此操作。在这里,单击Add...
按钮。导航到XML模式文件,然后选择OK
。如果关闭并重新打开XML文件,则将具有自动补全功能。最后,当解压缩
ColorOptionsView.java
中使用的属性时,作者可以专门从此命名空间中查找属性。这是从同一来源(我评论)://grab the declared-styleable resource entries
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Options, 0, 0);
//get the "titleText" entry from this element's attributes
String titleText = a.getString(R.styleable.Options_titleText);
//get the "valueColor" attribute. If it does not exists, set the default to holo_blue_light
int valueColor = a.getColor(R.styleable.Options_valueColor,
android.R.color.holo_blue_light);
a.recycle();
关于android - 可声明样式名称如何链接到使用其属性的 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16153976/