2011-11-20 31 views
3

在我的应用程序中,我使用addView动态添加视图到布局。
添加的观点预先充气使用使用addView时Android会忽略match_parent

andorid:layout_width="match_parent" 

然而,加法用

parentView.addView(view, index); 

的视图的视图addded但这么想的填充后的家长的宽度
我想这是因为预先完成“match_parent”大小的计算, 当视图膨胀时,因此它没有提及如何设置corect宽度
是这种情况吗?
有没有办法告诉视图重新计算布局参数?

我的代码:
activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/blue_bg"> 
    <ImageView android:id="@+id/myImg"  
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:src="@drawable/myImg" 
     android:scaleType="fitXY"/> 
    <LinearLayout android:id="@+id/addressItemContainer" 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_below="@id/myImg" android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp"> 
     <View android:id="@+id/placeHolder" 
      android:layout_width="match_parent" android:layout_height="match_parent"/> 
    </LinearLayout> 
</RelativeLayout> 

inserted_view.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:background="@drawable/blue_selector"> 
.... internal views 
</LinearLayout> 

java代码:

LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View view = li.inflate(R.layout.inserted_view, null); 
... fill inserted view fields ... 
View aiv = findViewById(R.id.placeHolder); 
ViewGroup parent = (ViewGroup) aiv.getParent(); 
int index = parent.indexOfChild(aiv); 
parent.removeView(aiv); 
parent.addView(view, index); 

的Tx的帮助:)

回答

11

好吧,只是添加以下代码

View aiv = findViewById(R.id.placeHolder); 
aiv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

标记为已解决,请。

+0

对不起,没有工作。我通过在xml中添加一个刺查视图parralel到占位符视图来解决它 – Tomer

+0

不工作,对答案没有解释,对最高选民的任何解释? –