0

Necesito agregar dinamincamente a mi actividad principal (una n cantidad) una vista que tengo definida en un archivo xml en la carpeta layouts.Tengo defida la vista personalizada como un RelativeLayout donde tengo difrentes tipos de vistas. El problema que tengo es que tras inflar la vista personalizada y gregar la la actividad principal los elementos de la vista personalizada quedan desordenados y todos superpuestos en el centro de mi mainactivity
este es el archivo xml de mi vista personalizada:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
>
<ImageView
    android:layout_width="100dp"
    android:layout_height="match_parent"
    android:id="@+id/imageView"
     />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_marginStart="100dp"
    android:layout_height="100dp">

    <TextView
        android:text="TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="11dp"
        android:layout_marginTop="27dp"
        android:id="@+id/textView2"
        />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="47dp"
        android:id="@+id/button" />

    <TextView
        android:text="TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2" />

    <TextView
        android:text="TextView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:layout_alignBottom="@+id/button"
        android:layout_alignStart="@+id/textView2" />
</RelativeLayout>

de esta forma agrego la vista a la actividad principal

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vistaPadre=(LinearLayout)findViewById(R.id.mainLinear);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.vistapersonalizada;
vistaPadre = (LinearLayout) findViewById(R.id.mainLinear);
layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
vistaPadre.addView(layoutPlantilla);

aqui dejo una captura de como queda agregada la vista a la actividad principal vista personalizada agregada al layout principal

1 respuesta 1

1

Buenas compañero E.B, probando el codigo pude genera el mismo resultado que muestras, esto ocurre porque tu xml principal es un linearlayout y tu personalizado es un relative layout en lo cual sus elementos usan la posición de estos para su reordenamiento, el inconveniente lo puede solucionar cambiando el tipo de layout en tu xml principal, como ejemplo te dejo como lo hice.

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.datamex.jsanchez.relative.MainActivity">

LinearLayout>

Paso a ser:

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.datamex.jsanchez.relative.MainActivity">

RelativeLayout>

Y esto también se deberá cambiar para que coincida el tipo de objeto:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vistaPadre=(LinearLayout)findViewById(R.id.mainLinear);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.vistapersonalizada;
vistaPadre = (LinearLayout) findViewById(R.id.mainLinear);
layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
vistaPadre.addView(layoutPlantilla);

a esto:

    vistaPadre=(RelativeLayout)findViewById(R.id.activity_main);
    LayoutInflater inflar= LayoutInflater.from(this);
    int id = R.layout.mainlinear;
    vistaPadre = (RelativeLayout) findViewById(R.id.activity_main);
    layoutPlantilla = (RelativeLayout) inflar.inflate(id,null,false);
    vistaPadre.addView(layoutPlantilla);

Espero que te sirva la respuesta, favor avisar cualquier error para ver si podemos ayudarte.Saludos.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.