2014-07-17 4 views
1

내가이 열거를 소비하고 다시 사용하여 사용자 정의 형식

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

으로 사용자 정의 XML을 열거 정의 고려 형식 (사용자 지정 열거 형), 어떻게 그것을 얻을 수 있습니다. 예를 들어

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

같은 형식 Dimension 모두 같은 여백 - 왼쪽 여백 오른쪽, 여백 - 상단 여백 하단 생각 해보자. 마찬가지로 형식을 정의하고 같은 선언 스타일로 다른 특성에 사용하려고합니다.

도움 주셔서 감사합니다.

+2

솔루션의 존재 여부는 의심 스럽습니다. 모양이 프레임 워크에서 사용되는 방식을 가졌습니다. enum은'layout_width'와'layout_height' 정의에서 반복됩니다. 이 [attrs.xml] (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/frameworks/base/core/)에서 확인할 수 있습니다. res/res/values ​​/ attrs.xml? av = f) –

답변

4

해결책이 없습니다. 그러한 enum 정의가 이 아니고이 다시 사용되어야하는 이유입니다. 모든 속성에 대해 별도의 가능한 값 집합을 정의해야합니다.

<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 />이 그들 각각에 대해 정의 된대로 : 당신은 안드로이드 attrs.xml으로 볼 때 분명하다. 이것은 다음과 같은 방식으로 사용됩니다 :

<declare-styleable name="ViewGroup_MarginLayout"> 
    <attr name="layout_width" /> 
    <attr name="layout_height" /> 
    ... 
</declare-styleable> 

그래서 당신이하려는 것은 불가능한 것 같습니다.