2011-11-20 72 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

不工作,對答案沒有解釋,對最高選民的任何解釋? –