2010-07-26 9 views
28

कैसे जो ViewGroup (लेआउट) में जोड़ा जाता है विशेष रूप से देख सकते हैं या ViewGroup के सूचकांक

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

कैसे जो "llIndex" है आईडी के रूप में LinearLayout के सूचकांक को पुनः प्राप्त करने के लिए। धन्यवाद

उत्तर

0

जब आप एक्सएमएल बनाते हैं, तो वहां एक फ़ाइल है yourpackage.generated.R.java जो सभी आईडी/ड्रॉबल्स/आदि के साथ बनाई गई है। तो यह संदर्भित करने के लिए, अपने वर्ग में उत्पन्न R.java आयात करते हैं, तो ऐसा करने (यह मानते हुए कि आप एक गतिविधि में हैं):

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

आपकी प्रतिक्रिया के लिए धन्यवाद ... लेकिन असल में मैं इंडेक्स चाहता हूं, अब इस उदाहरण में रैखिक लयआउट की अनुक्रमणिका जिसमें आईडी के रूप में "llIndex" है 3। मैं इसे एपीआई के माध्यम से पढ़ना चाहता हूं। – user373885

+0

ठीक है मुझे लगता है कि मैं उलझन में हूं क्योंकि आप वास्तव में क्या चाहते हैं? क्या llAddNote किसी अन्य दृश्य में निहित है, और आपको यह जानने की आवश्यकता है कि यह कौन सा बच्चा # है? –

99

बहुत ही सरल - दृश्य के माता-पिता पर indexOfChild फ़ंक्शन को कॉल करें:

LinearLayout addNoteLayout = (LinearLayout) findViewById(R.id.llAddNote); 
int index = ((ViewGroup) addNoteLayout.getParent()).indexOfChild(addNoteLayout); 
संबंधित मुद्दे