2014-02-11 37 views
0

我的代碼:如何將ImageView的添加到屏幕的中心,比

Display display = getWindowManager().getDefaultDisplay(); 
final int width = display.getWidth(); 
final int height = display.getHeight(); 

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200,200); 
relativeLayout.addView(img1,params); 
params.leftMargin = width/2; 
params.topMargin = height/2; 
setContentView(relativeLayout); 

有人能幫助我適應我IMG1屏幕的中心,在每個分辨率?
我認爲display.getwidth值與params.leftmargin沒有關聯。
請指示我通過除以得到任何用途的保證金值。

+0

這是你的答案(真正的[一](http://stackoverflow.com/a/3985891/2649012)) –

回答

1

試試這個

RelativeLayout.LayoutParams params =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); 
img1.setLayoutParams(params); 
relativeLayout.addView(img1); 

編輯:如果u要添加更多的觀點則u應該創建layoutparams對每個視圖和use addRule方法餘米只是給U上的一個例子,如何放置視圖視圖下ü可以把這個觀點right , left or above accoridngly改變這種RelativeLayout.ALIGN_BOTTOM和u可以使用RelativeLayout.LAYOUT_BELOW代替RelativeLayout.ALIGN_BOTTOM

p.addRule(RelativeLayout.ALIGN_BOTTOM, ur_view.getId()); 

如果u在XML創建它,它會更容易

如果u要設置保證金任何觀點做這樣的事情

p.setMargins(left margin, top margin, right margin, bottom margin); 

左邊距,上邊距,右邊距,底邊距是整數 值,其值U希望烏拉圭回合視圖設置

+0

Yessss,絕對是我想要的答案..非常感謝你... :) –

+0

但是,不要煩擾我先生。我仍然想要在不同位置的相關佈局上找到其他圖片。我怎麼能夠 ? –

+0

你可以發佈一個屏幕截圖和整個場景什麼你想要和遲到回覆對不起 – kId

相關問題