2013-09-16 7 views
17

私はViewAとViewBの両方に "title"タグを持たせたいと思います。しかし、私はattrs.xmlでこれを置くことができない。異なるタグに同じ名前のスタイル属性をいくつか宣言するにはどうすればいいですか?

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" format="string" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

エラーが原因属性「タイトル」、すでにを定義されているの。 Another questionこの溶液を示す:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewB"> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

その場合に、R.styleable.ViewA_titleR.styleable.ViewB_titleは発生しません。次のコードを使用してAttributeSetから属性を読み取るために必要です。

TypedArray a=getContext().obtainStyledAttributes(as, R.styleable.ViewA); 
String title = a.getString(R.styleable.ViewA_title); 

どうすればこの問題を解決できますか?

+0

類似しているhttp://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch

答えて

32

を使用する必要があります。これは、あなたが示唆するものである:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="title" format="string" /> 
    <declare-styleable name="ViewA"> 
     <attr name="title" /> 
    </declare-styleable> 
    <declare-styleable name="ViewB"> 
     <attr name="title" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

は今、R.styleable.ViewA_titleR.styleable.ViewB_titleは両方にアクセスできます。

チャンスがあれば、Linkを読んでください。関連する見積もり:

要素を上部要素または 要素内に定義できます。もし私がより多くの でattrを使うつもりなら、私はそれをルート要素に入れます。

+0

これはもう真実ではないようです(少なくともAndroid Studio 2.3を使用している場合)。ルートに属性を定義すると、そのリソースを解決できないというエラーが表示されます。あなたは共有属性を持つ親を定義する必要があると思います。 – BioeJD

+0

Androidスタジオでは動作しません。 – user3269713

+0

2018-2現在の正解はhttps://stackoverflow.com/a/43635002/1963973です – marianosimone

3

あなたが投稿したリンクはあなたに正しい答えを与えてい継承

Javaコードで
<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

String namespace = "http://schemas.android.com/apk/res/" + getContext().getPackageName(); 
int title_resource = attrs.getAttributeResourceValue(namespace, "title", 0); 
String title = ""; 
if(title_resource!=0){ 
    title = getContext().getString(title_resource); 
} 

int min = attrs.getAttributeResourceValue(namespace, "min", 0); // read int value 
int max = attrs.getAttributeResourceValue(namespace, "max", 0); 
+0

これは動作しません。 ViewB_titleは生成されません。 IDがViewB_minと衝突するため、ViewA要素をViewB要素に使用することはできません。 – Andreas

2

代わりにこれを行います。何parentタグはかつてtitleViewAに宣言されているので、これは、それは別のdeclare-styleable

1
<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" parent="ViewA"> // inherit from ViewA 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

に再び内で宣言する(することはできませんもして)必要はありませんされて

<resources> 
    <declare-styleable name="ViewA"> 
     <attr name="title" format="string" /> 
    </declare-styleable> 

    <declare-styleable name="ViewB" > 
     <attr name="title" /> 
     <attr name="min" format="integer" /> 
     <attr name="max" format="integer" /> 
    </declare-styleable> 
</resources> 

を必要としません

これは動作しません。

<resources> 
<attr name="title" format="string" /> 
<declare-styleable name="ViewA"> 
    <attr name="title" /> 
</declare-styleable> 
<declare-styleable name="ViewB"> 
    <attr name="title" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

また、これは動作しません。

<resources> 
<declare-styleable name="ViewA"> 
    <attr name="title" format="string" /> 
</declare-styleable> 

<declare-styleable name="ViewB" > 
    <attr name="title" /> 
    <attr name="min" format="integer" /> 
    <attr name="max" format="integer" /> 
</declare-styleable> 

これはOKだった!!

関連する問題