2011-11-02 98 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