2011-11-02 48 views
0

我動態充氣下面的XML添加經由膨脹屬性相對佈局被忽略

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dip" 
    > 
    <ImageView 
    android:id="@+id/grid_img" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:scaleType="fitXY" 
    android:src="@drawable/nature3"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/img_name" 
    android:text=",jfjgf xxfbdg fd dfhs lhl;fsfladsfh; jkhsdl;fshkjfl;fks; l;hflsaf" 
    android:inputType="textMultiLine" 
    android:maxLines="2" 
    android:ellipsize="end" 
    android:layout_alignBottom="@+id/grid_img" 
    android:layout_alignRight="@+id/grid_img" 
    android:layout_alignLeft="@+id/grid_img" 
    /> 
</RelativeLayout> 

我經由通貨膨脹使用以下代碼添加該XML。

RelativeLayout v = (RelativeLayout)inf.inflate(R.layout.ui_three, null); 

現在的問題是我在xml中定義的屬性被忽略,我得到的android手機上的followin UI。

enter image description here

我得到的TextView與在以XML格式它與圖像的底部對齊圖像的頂部邊緣對齊。

對此問題的任何解決方案。

+0

嘗試的FrameLayout代替。 –

回答

2

傳遞父佈局,您是要在新的佈局添加到作爲第二個參數:

RelativeLayout v = (RelativeLayout)inf.inflate(R.layout.ui_three, parent); 

膨脹的佈局則將會從父繼承佈局PARAMS。如果您收到錯誤,當你再嘗試新的佈局添加到其父,使用替代LayoutInflator方法,它允許你從自動粘貼新的佈局父停止充氣:

RelativeLayout v = (RelativeLayout)inf.inflate(R.layout.ui_three, parent, false); 
+0

感謝Mark Allison你告訴我的第二個選擇。 –

+0

謝謝,正是我在找的! – Seb83