在android xml中重用自定义格式

时间:2014-07-17 07:15:59

标签: android xml enums

考虑定义为

的自定义xml枚举
<attr name="myProperty">
    <enum name="None" value="0"/>
    <enum name="One" value="1"/>
    <enum name="Two" value="2"/>
    <enum name="Three" value="3"/>
    <enum name="Four" value="4"/>
    <enum name="Five" value="5"/>
    <enum name="Six" value="6"/>
    <enum name="Seven" value="7"/>
    <enum name="Eight" value="8"/>
    <enum name="Nine" value="9"/>
    <enum name="Ten" value="10"/>
</attr>

我正在使用此枚举,如下所示

<declare-styleable name="MyUnrelatedControl">
    <attr name="myProperty" />
</declare-stylable>

但问题是假设我有两个相同格式的属性(自定义枚举),我该如何实现。例如

<declare-styleable name="MyUnrelatedControl">
    <attr name="unrelatedControl1" /> <!-- format=myProperty -->
    <attr name="unrelatedControl2" /> <!-- format=myProperty -->
</declare-stylable>

考虑这一点,如margin-left,margin-right,margin-top,margin-bottom,所有格式都相同Dimension。类似地,我想定义一种格式,并将其用于同一declare-stylable中的不同属性。

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

没有解决方案。原因是这样的枚举定义应该被重用。对于每个属性,您需要定义一组单独的可能值。当您研究Androids attrs.xml

时,这一点很明显
<declare-styleable name="ViewGroup_Layout">
    <!-- Specifies the basic width of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant width or one of
         the special constants. -->
    <attr name="layout_width" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>

    <!-- Specifies the basic height of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant height or one of
         the special constants. -->
    <attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
</declare-styleable>

正如您所看到的,layout_widthlayout_height完全相同,但为每个<attr />定义了单独的<declare-styleable name="ViewGroup_MarginLayout"> <attr name="layout_width" /> <attr name="layout_height" /> ... </declare-styleable> 。然后以与您尝试的方式大致相同的方式使用它:

{{1}}

所以看来你要做的事情是不可能的。