使用布局定位问题Inflate

时间:2011-12-19 16:47:51

标签: android inflate

我有一个布局A,由一组2个文本和一个图像组成,后跟另一组2个文本和一个图像(它们用一条线分隔)。我想夸大这个布局(比方说A)并将其放入另一个布局(比方说B)。问题是图像和分隔符都在B的中间,而我想要它们在A的中间。我的代码是:

布局A(要充气):

<?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>

我正在插入膨胀的布局:

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);

请注意,虽然为图片设置了属性android:layout_height="fill_parent"android:layout_centerHorizontal="true",但我希望它们以A而不是B为中心。

任何帮助??

1 个答案:

答案 0 :(得分:1)

此处找到的解决方案:How to use layoutinflator to add views at runtime? 解决了我的问题。

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