【问题标题】:Problems positioning with layout Inflate布局膨胀定位问题
【发布时间】:2012-01-13 23:34:14
【问题描述】:

我的布局 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。

有什么帮助吗??

【问题讨论】:

    标签: android inflate


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多