studio linearlayout left example ejemplo constraint below java android android-layout android-linearlayout

java - left - Creación de LinearLayout mediante programación/dinámica con varias vistas



relative layout (4)

Tengo una jerarquía que es así:

  • LinearLayout (horizontal)
    • ImageView
    • LinearLayout (vertical)
      • Vista de texto
      • Vista de texto
      • Vista de texto
      • Vista de texto

Quiero poder agregar la jerarquía anterior mediante iteración siempre que haya datos que se puedan obtener de la base de datos (usando Parse )

He intentado colocar ImageView y LinearLayout debajo de LinearLayout principal, pero no parece funcionar. Aquí está mi código en MainActivity.Java:

LinearLayout LL_Outer = (LinearLayout) findViewById(R.id.new_linearLayoutOuter); LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation LL_Outer.setBackgroundColor(color.white); // set background // set Layout_Width and Layout_Height LinearLayout.LayoutParams layoutForOuter = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); LL_Outer.setLayoutParams(layoutForOuter); LinearLayout LL_Inner = (LinearLayout) findViewById(R.id.new_linearLayoutInner); LL_Inner.setOrientation(LinearLayout.HORIZONTAL); LL_Inner.setBackgroundColor(color.white); LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); LL_Inner.setLayoutParams(layoutForInner); //LL_Outer.addView(LL_Inner); ImageView IV = (ImageView) findViewById(R.id.new_imageViewPP); //IV.getLayoutParams().height = 55; //IV.getLayoutParams().width = 55; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.setMargins(14, 12, 0, 0); params.height = 55; params.weight = 55; IV.setBackgroundColor(color.black); IV.setLayoutParams(params); LL_Inner.addView(IV); LL_Outer.addView(LL_Inner);

No sé dónde me equivoqué porque mi código no generó ningún error. Por favor ayuda.

EDITAR: He editado las Orientaciones en consecuencia y cuando ejecuto la aplicación, deja de funcionar. Y muestra un error en LogCat que dice "El niño especificado ya tiene un padre. Primero debe llamar removeView () en el padre del niño.

Aquí está mi XML:

<LinearLayout android:id="@+id/new_linearLayoutOuter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="horizontal" > <ImageView android:id="@+id/new_imageViewPP" android:layout_width="55dp" android:layout_height="55dp" android:layout_marginLeft="14dp" android:layout_marginTop="12dp" android:background="@color/black" android:contentDescription="@string/pp" /> <LinearLayout android:id="@+id/new_linearLayoutInner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" > <TextView android:id="@+id/new_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/movie_title" android:paddingTop="10dp" android:paddingLeft="7dp" android:textSize="15sp" /> <!-- Title of the movie --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/review_by" android:paddingTop="3dp" android:paddingLeft="7dp" android:textSize="12sp" /> <!-- By --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/movie_stars" android:paddingTop="3dp" android:paddingLeft="7dp" android:textSize="12sp" /> <!-- Rating and date --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sample_string" android:maxLines="5" android:scrollbars="vertical" android:paddingTop="10dp" android:paddingLeft="7dp" android:textSize="12sp" android:layout_gravity="center_vertical|right" /> <!-- Review content --> </LinearLayout> </LinearLayout>


El problema es porque las vistas ya están agregadas al diseño en el archivo XML . Luego, encuentra findViewById (encuentra) e intenta agregarlos nuevamente al diseño. Es por eso que la aplicación falla al quejarse, la vista ya tiene un padre y no se puede agregar de nuevo.


En el archivo XML LinearLayout ya tiene vista secundaria. Así que no hay necesidad de agregarlos en el código.


Le sugiero que elimine el archivo xml y solo use el código completo en el lado de Java. Puedes agregar las vistas programáticamente desde el lado de Java. este de xtreemdeveloper pero cambio pocas líneas para el diseño principal.

// remove this params set it up below parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // change the code above into .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); addContentView(parent,layoutParams); // end of my change

se verá así en código completo =

LinearLayout parent = new LinearLayout(context); // remove this params set it up below parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // change the code above into .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); addContentView(parent,layoutParams); // end of my change parent.setOrientation(LinearLayout.HORIZONTAL); //children of parent linearlayout ImageView iv = new ImageView(context); LinearLayout layout2 = new LinearLayout(context); layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); layout2.setOrientation(LinearLayout.VERTICAL); parent.addView(iv); parent.addView(layout2); //children of layout2 LinearLayout TextView tv1 = new TextView(context); TextView tv2 = new TextView(context); TextView tv3 = new TextView(context); TextView tv4 = new TextView(context); layout2.addView(tv1); layout2.addView(tv2); layout2.addView(tv3); layout2.addView(tv4);


Quieres esa jerarquía programáticamente.

- LinearLayout(horizontal) - ImageView - LinearLayout(vertical) - TextView - TextView - TextView - TextView

Ok, comencemos con Parent LinearLayout

LinearLayout parent = new LinearLayout(context); parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); parent.setOrientation(LinearLayout.HORIZONTAL); //children of parent linearlayout ImageView iv = new ImageView(context); LinearLayout layout2 = new LinearLayout(context); layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); layout2.setOrientation(LinearLayout.VERTICAL); parent.addView(iv); parent.addView(layout2); //children of layout2 LinearLayout TextView tv1 = new TextView(context); TextView tv2 = new TextView(context); TextView tv3 = new TextView(context); TextView tv4 = new TextView(context); layout2.addView(tv1); layout2.addView(tv2); layout2.addView(tv3); layout2.addView(tv4);

Y has terminado :)