2010-07-26 7 views
28

Wie den Index bestimmte Ansicht OR Viewgroup erhalten, die mit dem Viewgroup (Layout) hinzugefügt wird

<TextView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:layout_margin="24dip" 
    android:text="Add Notes" /> 
<TableLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:layout_marginLeft="24dip" 
    android:layout_marginRight="24dip" android:id="@+id/tlNotes" 
    android:stretchColumns="0"> 
</TableLayout> 

<Button android:id="@+id/bAddNoteLine" 
    android:layout_marginLeft="24dip" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:text="ADD"> 
</Button> 

<LinearLayout android:id="@+id/llIndex" 
    android:orientation="horizontal" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:layout_margin="21dip" 
    android:gravity="center"> 
    <Button android:id="@+id/bSaveSubjectiveNote" 
     android:layout_width="192dip" android:layout_height="wrap_content" 
     android:text="Save" /> 
    <Button android:id="@+id/bDiscardSubjectiveNote" 
     android:layout_width="192dip" android:layout_height="wrap_content" 
     android:layout_marginLeft="48dip" android:background="@drawable/button" 
     android:text="Discard" /> 
</LinearLayout> 

wie der Index der Linearlayout abzurufen, die „llIndex“, wie id hat. Danke

Antwort

0

Wenn Sie das XML erstellen, gibt es eine Datei yourpackage.generated.R.java, die mit allen IDs/Drawables/etc darin erstellt wird. So ist es zu verweisen, importieren Sie die erzeugte R.java in Ihrer Klasse, dann dies tun (vorausgesetzt, Sie in einer Aktivität sind):

setContentView(R.layout.my_content); 
LinearLayout addNoteLayout = (LinearLayout) findViewById(R.id.llAddNote); 
+0

Danke für Ihre Antwort ... Aber eigentlich möchte ich Index, jetzt in diesem Beispiel der Index von linearLayout, die "llIndex" als ID hat, ist 3. Ich möchte es durch API lesen. – user373885

+0

Nun ich denke, ich bin verwirrt, weil was genau willst du den Index von? Ist llAddNote in einer anderen Ansicht enthalten und Sie müssen wissen, welches child # es ist? –

99

Sehr einfach - rufen Sie die indexOfChild Funktion auf der Eltern-Ansicht:

LinearLayout addNoteLayout = (LinearLayout) findViewById(R.id.llAddNote); 
int index = ((ViewGroup) addNoteLayout.getParent()).indexOfChild(addNoteLayout); 
Verwandte Themen