Подтвердить что ты не робот

Как получить индекс определенного вида OR 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>

как получить индекс linearLayout, который имеет "llIndex" как id. Благодаря

4b9b3361

Ответ 1

Очень просто - вызовите функцию indexOfChild в родительском представлении:

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

Ответ 2

Когда вы создаете XML, есть файл yourpackage.generated.R.java, который создается со всеми идентификаторами /drawables/etc в нем. Чтобы ссылаться на него, импортируйте созданный R.java в свой класс, затем сделайте это (если вы находитесь в Activity):

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