Programming

사용자 정의보기를 위해 attrs.xml에서 동일한 이름의 속성

procodes 2020. 6. 2. 22:05
반응형

사용자 정의보기를 위해 attrs.xml에서 동일한 이름의 속성


동일한 이름의 속성을 공유하는 몇 가지 사용자 정의보기를 작성하고 있습니다. 해당 <declare-styleable>섹션에서 attrs.xml속성에 동일한 이름을 사용하고 싶습니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView1">
        <attr name="myattr1" format="string" />
        <attr name="myattr2" format="dimension" />
        ...
    </declare-styleable>

    <declare-styleable name="MyView2">
        <attr name="myattr1" format="string" />
        <attr name="myattr2" format="dimension" />
        ...
    </declare-styleable>
</resources>

나는 그 말을하여 오류가 발생 myattr1하고 myattr2이미 정의되어 있습니다. 대한 format속성을 생략해야한다는 것을 알았지 만 그렇게하면 콘솔에서 다음 오류가 발생합니다.myattr1myattr2MyView2

[2010-12-13 23:53:11 - MyProject] ERROR: In <declare-styleable> MyView2, unable to find attribute 

내가 이것을 달성 할 수있는 방법이 있습니까? 어쩌면 일종의 네임 스페이스 (추측)입니다.


솔루션 : 두보기에서 공통 속성을 추출하여 <resources>노드의 하위로 직접 추가 하십시오.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myattr1" format="string" />
    <attr name="myattr2" format="dimension" />

    <declare-styleable name="MyView1">
        <attr name="myattr1" />
        <attr name="myattr2" />
        ...
    </declare-styleable>

    <declare-styleable name="MyView2">
        <attr name="myattr1" />
        <attr name="myattr2" />
        ...
    </declare-styleable>
</resources>

위에 게시 된 솔루션이 Android Studio에서 나에게 적합하지 않으므로이 답변을 게시하고 있습니다. 사용자 정의보기에서 내 사용자 정의 속성을 공유해야하므로 Android Studio에서 위의 솔루션을 시도했지만 운이 없었습니다. 그래서 나는 실험하고 그것을 할 길을갑니다. 누군가가 같은 문제를 찾는 데 도움이되기를 바랍니다.

  <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <!-- parent styleable -->
     <declare-styleable name="MyView">
         <attr name="myattr1" format="string" />
         <attr name="myattr2" format="dimension" />
     </declare-styleable>

     <!-- inheriting parent styleable -->
     <!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
    <declare-styleable name="MyView1" parent="MyView">
        <attr name="myattr1" />
        <attr name="myattr2" />
        <attr name="myBackgroundColor" format="color"/>
    </declare-styleable>


    <!-- inheriting parent styleable -->
    <!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
    <declare-styleable name="MyView2" parent="MyView">
        <attr name="myattr1" />
        <attr name="myattr2" />
        <attr name="myfont" format="string"/>
        ...
    </declare-styleable>
</resources>

이것은 나를 위해 완전히 작동합니다. 우리는 Parent를 스타일링해야하고, 그 부모 스타일을 상속 받아야합니다. 예를 들어, 위에서 한 것처럼 : Parent styleable name MyView이고 이것을 MyView1 및 MyView2와 같은 다른 스타일로 상속했습니다.


프리 야 싱할 (Priya Singhal)이 대답 한 것처럼 Android Studio에서는 공통 속성 이름을 고유 한 스타일 이름으로 정의해야합니다. 그들은 더 이상 뿌리에있을 수 없습니다.

그러나 주목해야 할 몇 가지 다른 사항이 있습니다 (따라서 답변을 추가하는 이유도 있음).

  • The common styles don't need to be named the same thing as a view. (Thanks to this answer for pointing that out.)
  • You don't need to use inheritance with a parent.

Example

Here is what I did in a recent project that has two custom views that both share the same attributes. As long as the custom views still have the names for the attributes and don't include a format, I can still access them as normal from code.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- common attributes to all custom text based views -->

    <declare-styleable name="TextAttributes">
        <attr name="text" format="string"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>
        <attr name="gravity">
            <flag name="top" value="48" />
            <flag name="center" value="17" />
            <flag name="bottom" value="80" />
        </attr>
    </declare-styleable>

    <!-- custom text views -->

    <declare-styleable name="View1">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

    <declare-styleable name="View2">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

</resources>

Streamlined example

In fact, I don't even need to put the attributes under a custom name. As long as I define them (give them a format) for at least one custom view, I can use them anywhere (without the format). So this also works (and looks cleaner):

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="View1">
        <attr name="text" format="string"/>
        <attr name="textSize" format="dimension"/>
        <attr name="textColor" format="color"/>
        <attr name="gravity">
            <flag name="top" value="48" />
            <flag name="center" value="17" />
            <flag name="bottom" value="80" />
        </attr>
    </declare-styleable>

    <declare-styleable name="View2">
        <attr name="text"/>
        <attr name="textSize"/>
        <attr name="textColor"/>
        <attr name="gravity"/>
    </declare-styleable>

</resources>

For a large project, though, this could get messy and defining them at the top in a single location might be better (as recommended here).


Thanks Lewis I had the same problem , and your inheritance solution gave me the hint for doing it like below and it works fine.I just declared common attributes at the above and rewrite it in the body of style declaration again without formatting. I hope it helps someone

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- common attributes -->
     <attr name="myattr1" format="string" />
     <attr name="myattr2" format="dimension" />

 <!-- also note "myBackgroundColor" belongs to child styleable"MyView1"-->
<declare-styleable name="MyView1" >
    <attr name="myattr1" />
    <attr name="myattr2" />
    <attr name="myBackgroundColor" format="color"/>
</declare-styleable>

<!-- same way here "myfonnt" belongs to child styelable "MyView2" -->
<declare-styleable name="MyView2" parent="MyView">
    <attr name="myattr1" />
    <attr name="myattr2" />
    <attr name="myfont" format="string"/>
    ...
</declare-styleable>


Just in case someone still stuck with this problem after tried available solution. I stuck with add subtitle attribute with string format.

My solution is remove the format.

before:

<attr name="subtitle" format="string"/>

after:

<attr name="subtitle"/>

참고URL : https://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view

반응형