Pregunta

Aquí está mi diseño de Prent (main.xml)

<!-- ============================================================ -->
            <!--  All Employees are added here (added and More then one) -->
            <!-- ============================================================ -->
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/myLayout"
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000">

                <!-- ++++++++ here extra layout is added programmatically -->


            </LinearLayout>

Aquí está mi otro archivo de diseño que quiero como niño, digamos child.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"         
    android:layout_alignParentLeft="true"         
    android:layout_alignParentTop="true"         
    android:gravity="center_vertical"         
    android:orientation="horizontal" >          

    <Button             
        android:id="@+id/btn_left"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content" 
        android:singleLine="false"   
        android:maxLines="2"         
        android:text="hello"/>
    <TextView             
        android:id="@+id/list_title"             
        android:layout_width="fill_parent"             
        android:layout_height="wrap_content"             
        android:layout_weight="0.5"             
        android:gravity="center_horizontal"             
        android:text="Photos"             
        android:textAppearance="?android:attr/textAppearanceLarge" />          
    <Button             
        android:id="@+id/btn_right"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content"             
        android:text="Save" />     
</LinearLayout>

Ahora, en mi actividad principal, llamo a Main.xml y, en función de la condición de bucle for For, quiero agregar el diseño de Child.xml y cada vez que quiero establecer el valor diferente del TextView de Child.xml

Entonces, ¿cómo es posible?

He hecho así:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout);
    }
}

Pero después de hacer eso recibí un error de sintaxis en .inflate y en .addchild

Entonces, ¿a dónde me he equivocado? Ayúdame por algún código para agregarlo por bucle.

¿Fue útil?

Solución

Simplemente cree un bucle como a continuación:

    LinearLayout parent=findViewById(R.id.myLayout);
    for(int i=0;i<list.size();i++)
    {
       LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
       TextView txt= (TextView)llItem.findViewById(R.id.title);
       txt.setText(list.get(i));
       parent.add(llItem);
    }

Otros consejos

Inflar la vista y luego agregue al padre

LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);

Repita tantas veces como sea necesario o use un bucle

Si entendí su pregunta correctamente, debe mostrar el botón, la vista text y el botón uno al lado del otro en su diseño. Si desea esto, simplemente tome ListView y use un adaptador personalizado, con esto puede mostrar el botón, el texto y el botón uno al lado del otro

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top