211

同じ名前の属性を共有するカスタムビューをいくつか書いています。のそれぞれの<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>

myattr1myattr2はすでに定義されているというエラーが表示されます。とのformat属性を省略する必要があることがわかりましたが、そうすると、コンソールで次のエラーが発生します。myattr1myattr2MyView2

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

これを達成する方法はありますか、おそらくある種の名前空間(推測だけ)ですか?

4

5 に答える 5

444

解決策:両方のビューから共通の属性を抽出し、それらを<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>
于 2010-12-16T20:17:54.580 に答える
67

上記の解決策がAndroidStudioでうまくいかなかったため、この回答を投稿しています。カスタムビュー間でカスタム属性を共有する必要があるため、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>

これは私にとっては完全に機能します。親をスタイリング可能にする必要があり、次にその親をスタイリング可能にする必要があります。たとえば、上記で行ったように、親のスタイラブル名はMyViewであり、これをそれぞれMyView1やMyView2などの他のスタイラブルに継承します。

于 2015-10-12T09:18:27.497 に答える
30

Priya Singhalが回答したように、Android Studioでは、共通の属性名を独自のスタイル名で定義する必要があります。彼らはもう根元にいることはできません。

ただし、他にも注意すべき点がいくつかあります(そのため、私も回答を追加しています)。

  • 一般的なスタイルは、ビューと同じ名前にする必要はありません。(それを指摘してくれたこの答えに感謝します。)
  • 親との継承を使用する必要はありません。

これは、同じ属性を共有する2つのカスタムビューを持つ最近のプロジェクトで行ったことです。カスタムビューに属性の名前があり、が含まれていない限りformat、コードから通常どおりアクセスできます。

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

合理化された例

実際、属性をカスタム名で指定する必要はありません。format少なくとも1つのカスタムビューに対してそれらを定義する(それらにを与える)限り、どこでも(なしでformat)それらを使用できます。したがって、これも機能します(そしてきれいに見えます):

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

ただし、大規模なプロジェクトの場合、これは面倒になる可能性があり、単一の場所の上部に定義する方がよい場合があります(ここで推奨されています)。

于 2017-02-18T09:05:03.723 に答える
10

ルイスのおかげで私は同じ問題を抱えていました、そしてあなたの継承ソリューションは私にそれを以下のようにするためのヒントを与えました、そしてそれはうまくいきます。私は上記で共通の属性を宣言し、フォーマットせずにスタイル宣言の本文でそれを書き直しました。私はそれが誰かを助けることを願っています

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

于 2017-01-04T16:05:55.837 に答える
1

誰かが利用可能な解決策を試した後もこの問題で立ち往生している場合に備えて。私はフォーマットでsubtitle属性を追加することに固執しました。string

私の解決策は、フォーマットを削除することです。

前:

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

後:

<attr name="subtitle"/>

于 2016-08-09T10:25:39.110 に答える