1

I have a layout A composed by a set of 2 texts and one image followed by another set of 2 texts and one image (they are separated by a line). I want to inflate this layout (lets say A) and put it inside another one (lets say B). The problem is that the images and the separator are getting in the middle of B, while I wanted them in the middle of A. My code is:

Layout A (to be inflated):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:background="@drawable/bg_numeros"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/separator"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/divisoria_numeros" />

    <ImageView
        android:id="@+id/artistsDoll"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/separator"
        android:scaleType="fitCenter"
        android:src="@drawable/boneco_artistas_numeros" />

    <ImageView
        android:id="@+id/songsDoll"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="fitCenter"
        android:src="@drawable/bonecos_numeros" />

    <TextView
        android:id="@+id/songsNumbers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/artistsDoll"
        android:layout_toLeftOf="@+id/songsDoll"
        android:layout_toRightOf="@+id/separator"
        android:text="blabla"
        android:textColor="#333333"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/songsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/songsNumbers"
        android:layout_toLeftOf="@+id/songsDoll"
        android:layout_toRightOf="@+id/separator"
        android:text="SOME SONGS"
        android:textColor="#999999"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/artistsNumbers"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/artistsDoll"
        android:layout_toLeftOf="@+id/artistsDoll"
        android:text="blabla"
        android:textColor="#333333"
        android:textSize="14dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/artistsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/artistsNumbers"
        android:layout_toLeftOf="@+id/artistsDoll"
        android:text="SOME ARTISTS"
        android:textColor="#999999"
        android:textSize="11dp" />

</RelativeLayout>

I am inserting the inflated layout like this:

RelativeLayout A = (RelativeLayout) Patterns.mainContext
                      .getLayoutInflater()
                      .inflate(R.layout.A, null);

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, someTopLayout.getId());
B.addView(A, rlp);

Note that although the attributes android:layout_height="fill_parent" and android:layout_centerHorizontal="true" are set for the images, I expected them to centered in A, not B.

Any help??

Alesqui
  • 6,417
  • 5
  • 39
  • 43

1 Answers1

1

The solution found here: How to use layoutinflator to add views at runtime? Solved my problem.

RelativeLayout A = (RelativeLayout) Patterns.mainContext
                  .getLayoutInflater()
                  .inflate(R.layout.A, parentLayout, false);
Community
  • 1
  • 1
Alesqui
  • 6,417
  • 5
  • 39
  • 43