это мое первый раз прошу что-то здесь :)Как добавить последовательные ряды макеты один за другими в Android
Давайте перейдем к теме: Я пытаюсь сделать типичное «Нажмите здесь, чтобы добавить еще человек". «Лицо» - это другое поле, поэтому у меня уже есть 4 строки для ввода имени, но я хочу добавить один за другим до 8 (еще четыре, каждый раз, когда вы нажимаете кнопку «)».
Отладка: Когда я нажимаю макет, он станет пятым вкладчиком, но если я снова коснусь его, он больше не появится. Любая идея, как это исправить?
Это мой ".xml":
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip" >
<!-- Layout del form -->
<LinearLayout
android:id="@+id/InputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.85"
android:orientation="vertical" >
[...] //Some Layouts
<!-- Contributor 1 Layout -->
<LinearLayout
android:id="@+id/ContributorLayout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/NameLayout"
android:layout_marginBottom="15dip"
android:orientation="horizontal" >
<!-- Name Label -->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.45"
android:text="@string/Contributor_Name1_Str"
android:textSize="17dip" />
<!-- Input Name -->
<EditText
android:id="@+id/inputPart_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="0.55"
android:singleLine="true" />
</LinearLayout>
<!-- Contributor 2 Layout -->
[...] //Just as the Contributor 1
<!-- Contributor 3 Layout -->
[...] //Just as the Contributor 1
<!-- Contributor 4 Layout -->
[...] //Just as the Contributor 1
<!-- Contributor 5 Layout -->
<LinearLayout
android:id="@+id/ContributorLayout_5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ContributorLayout_4"
android:layout_marginBottom="15dip"
android:orientation="horizontal"
android:visibility="gone" >
<!-- Name Label -->
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.45"
android:text="@string/Contributor_Name5_Str"
android:textSize="17dip" />
<!-- Input Name -->
<EditText
android:id="@+id/inputContributor_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_weight="0.55"
android:singleLine="true" />
</LinearLayout>
<!-- Contributor 6 Layout -->
[...] //Just as the Contributor 5 // AKA Visibility = GONE
<!-- Contributor 7 Layout -->
[...] //Just as the Contributor 5 // AKA Visibility = GONE
<!-- Contributor 8 Layout -->
[...] //Just as the Contributor 5 // AKA Visibility = GONE
</LinearLayout>
<!-- Layout Inferior del form. -->
<LinearLayout
android:id="@+id/SubLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/InputLayout"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/Add_Colab_Field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip"
android:paddingLeft="25dip"
android:paddingRight="25dip"
android:orientation="horizontal"
android:clickable="true" >
<ImageView
android:id="@+id/add_event"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="left"
android:src="@drawable/ic_add_colab" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:layout_marginLeft="20dip"
android:text="@string/Add_Colab_Field"
android:textSize="17dip" />
</LinearLayout>
<!-- Button Create Event -->
<Button
android:id="@+id/btnAddEvent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Add_Colab_Field"
android:text="@string/Add_Event_Bttn" />
</LinearLayout>
</LinearLayout>
А вот мой "класс":
// Click en el botón add_colab_field
LinearLayout add_colab_field = (LinearLayout) findViewById(R.id.Add_Colab_Field);
add_colab_field.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final LinearLayout colab5 = (LinearLayout) findViewById(R.id.ContributorLayout_5);
final LinearLayout colab6 = (LinearLayout) findViewById(R.id.ContributorLayout_6);
final LinearLayout colab7 = (LinearLayout) findViewById(R.id.ContributorLayout_7);
final LinearLayout colab8 = (LinearLayout) findViewById(R.id.ContributorLayout_8);
// Si presionamos el botón añadir...
// Añadimos una nueva línea de participante
if (colab5.getVisibility() == View.GONE) {
colab5.setVisibility(View.VISIBLE);
} else if (colab5.getVisibility() == View.VISIBLE
&& colab6.getVisibility() == View.GONE) {
colab6.setVisibility(View.VISIBLE);
} else if (colab6.getVisibility() == View.VISIBLE
&& colab7.getVisibility() == View.GONE) {
colab7.setVisibility(View.VISIBLE);
} else if (colab7.getVisibility() == View.VISIBLE
&& colab8.getVisibility() == View.GONE) {
colab8.setVisibility(View.VISIBLE);
}
}
});
С наилучшими пожеланиями, Гильермо.
Это не ответ на ваш вопрос, а предложение: динамически добавлять макеты из кода по требованию. Не храните ненужные и повторяющиеся макеты в xml, так как они могут вообще не отображаться, но влияют на производительность и память отходов. Кроме того, прочитайте о том, как повторно использовать идентичные макеты с помощью http://developer.android.com/training/improving-layouts/reusing-layouts.html –