2011-11-02 84 views
1

按鈕後沒有表現出我想要動態地把一個位圖一個TextView和一個Button之間:安卓:動態視圖

<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:orientation="vertical"> 
</LinearLayout> 
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent" android:orientation="vertical"> 
    <Button android:layout_width="fill_parent" android:layout_gravity="fill_vertical" android:text="Button" android:layout_height="fill_parent" android:id="@+id/button1"> </Button> 
</LinearLayout> 

在我的主要活動創建我的自定義視圖,並把它添加到LinearLayout1

mView = new myView(this); 
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); 
ll.addView(rView); 

我的問題是,myView會消耗它下面的所有可用空間。

我錯過了什麼?

Thnx提前。

+0

嘗試添加視圖的佈局參數。 –

回答

2

嘗試爲您的自定義視圖動態設置佈局參數。

l1.addView(rView, new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT)); 

類似嘗試設置左,右位置動態

+0

非常感謝!這是我7小時搜索的解決方案! – Schwoabaseggele

+0

然後接受這個答案 – Karthi

1

添加視圖時,您應該添加的LayoutParams:你想將其添加到您應該在特定的位置

ll.addView(rView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

萬一使用:addView(child, index)

希望這會有所幫助!

+0

嗨Dimitris,我剛剛檢查了你的解決方案,但我的問題依然存在。因爲我需要我的視圖(其中包含一個正方形的位圖)在寬度*寬度大小下面的代碼給了我正確的畫面:'ll.addView(rView,new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,((WindowManager)getApplicationContext())。 getSystemService(Context.WINDOW_SERVICE))getDefaultDisplay()的getWidth()));' – Schwoabaseggele