2013-09-16 7 views
17

Ich möchte, dass sowohl ViewA als auch ViewB den Titel "title" haben. Aber ich kann das nicht in attrs.xml setzen:Wie deklarieren Sie mehrere Attribute mit demselben Namen für verschiedene Tags?

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

aufgrund des Fehlers Attribut „Titel“ ist bereits definiert. Another question zeigt diese Lösung:

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

aber in diesem Fall R.styleable.ViewA_title und R.styleable.ViewB_title werden nicht erzeugt. Ich brauche sie zum Lesen der Attribute aus dem AttributSet mit dem folgenden Code:

Wie kann ich das lösen?

+0

Ähnlich wie http://stackoverflow.com/questions/4434327/same-named-attributes-in-attrs-xml-for-custom-view – Suragch

Antwort

32

Der Link Sie auf dem Laufenden zu verwenden, haben nicht geben Ihnen die richtige Antwort. Dies ist, was es schon sagt Sie tun:

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

Nun R.styleable.ViewA_title und R.styleable.ViewB_title sind beide zugänglich.

Wenn Sie eine Chance haben, lesen Sie diese Antwort: Link. Relevante Zitat:

Sie können Attribute im oberen Element definieren oder innerhalb von ein Element. Wenn ich einen Attr in mehr als eine Stelle verwende, lege ich es in das Wurzelelement.

+0

Dies scheint nicht mehr wahr zu sein (oder zumindest bei Verwendung von Android Studio 2.3).Wenn ich Attribute im Stamm definiere, erhalte ich eine Fehlermeldung, dass diese Ressourcen nicht aufgelöst werden können. Ich denke, dass Sie ein Elternteil mit irgendwelchen geteilten Attributen definieren müssen. – BioeJD

+0

Es funktioniert jetzt nicht in Android Studio. – user3269713

+0

Die richtige Antwort ab 2018-2 ist https://Stackoverflow.com/a/43635002/1963973 – marianosimone

3

Sie benötigen Erbe

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

in Ihrem Java-Code

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

Dies funktioniert nicht. Es generiert ViewB_title nicht. ViewA_title kann nicht für ViewB-Elemente verwendet werden, da seine ID mit ViewB_min kollidieren würde. – Andreas

2

Tun Sie dies stattdessen. Kein parent Tag benötigt

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

Dies liegt daran, einmal title in ViewA deklariert ist, braucht es nicht (und kann auch nicht) in einem anderen 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> 

wieder erklärt werden

funktioniert das nicht.

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

Dies gilt auch nicht.

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

Das war OK !!

Verwandte Themen