2012-08-03 3 views
3

Je souhaite spécifier une relation entre deux vues dans mon fichier de mise en page XML Android. Voici ce que je veux faire:Android XML: Définir l'ID de ressource en tant que balise View dans le fichier de mise en page XML

<View 
     android:id="@+id/pathview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    ... 
    <CheckBox 
     android:id="@+id/viewPath" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:paddingRight="7dp" 
     android:shadowColor="#000000" 
     android:shadowDx="0.5" 
     android:shadowDy="0.5" 
     android:shadowRadius="0.5" 
     android:tag="@id/pathview" 
     android:text="Paths" /> 

Cependant, l'analyseur XML traite l'étiquette comme une chaîne et ne pas interpréter comme un entier (System.out.println((String) [viewPath].getTag()); affiche « false »). Est-il possible d'attribuer l'ID de ressource à la balise View?

+1

http://stackoverflow.com/questions/5100381/how-to-set-an-array-as-tag-to-any-view-through-layout-xml-in-android –

Répondre

1

Vous pouvez définir la chaîne d'ID comme une étiquette, puis obtenir l'ID.

certains comme:

<View 
    android:id="@+id/pathview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
... 
<CheckBox 
    android:id="@+id/viewPath" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:paddingRight="7dp" 
    android:shadowColor="#000000" 
    android:shadowDx="0.5" 
    android:shadowDy="0.5" 
    android:shadowRadius="0.5" 
    android:tag="pathview" 
    android:text="Paths" /> 

Puis dans votre code:

CheckBox viewPath = findViewById(R.id.pathview); 
String pathViewStrId = (String) viewPath.getTag(); 
int patViewId = getResources().getIdentifier(pathViewStrId, "id", getPackageName()); 
View pathView = findViewById(patViewId); 
2

Si vous utilisez

android:tag="?id/pathview" 

vous obtiendrez un identifiant de chaîne comme un entier décimal préfixé par une question marque. Je ne suis pas en mesure de trouver de la documentation pour ce comportement mais il semble assez stable. Qu'est-ce que vous faites demande l'ID pour le thème actuel. Pourquoi la chaîne résultante est alors préfixée avec un '?' est inconnu.

Par exemple:

Étant donné un certain identifiant,

public static final int test_id=0x7f080012; 

faire,

android:tag="?id/test_id" 

se traduira par une valeur de tag:

"?2131230738" 

Vous pouvez alors faire:

View otherView = activity.findViewById(
    Integer.parseInt(
     someView.getTag().toString().substring(1) 
    ) 
); 

Bien sûr, vous voudrez vérifier l'étiquette pour null et attraper NumberFormatException si vous écrivez une logique plus généralisée.

+0

bonne réponse! utilisé ceci pour intégrer les identifiants de ressources par défaut pour les tirages de boutons d'image, afin qu'ils puissent être déterminés par programmation. – NameSpace

Questions connexes