2010-07-21 75 views
0

所以我試圖添加一個imageview到我當前的xml設計 - 並且它的工作體面。現在我的主要問題是,我似乎無法找到一種方法來設置,如圖像屬性它需要顯示等設置相對佈局參數

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board); 

ImageView i = new ImageView(this); 
i.setImageResource(R.drawable.blue_1); 
i.setAdjustViewBounds(true); 
mRelativeLayout.addView(i); 
setContentView(mRelativeLayout); 

我試着用setlayoutparams瞎搞,但得到了完全不知道該怎麼辦用它。

回答

2

你確實有使用類的LayoutParams這樣做,高效,便捷地:

RelativeLayout mRelativeLayout = (RelativeLayout) findViewById(R.id.board); 
ImageView i = new ImageView(this); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40); 
params.leftMargin = 25; 
params.topMargin = 25; 
i.setImageResource(R.drawable.icon); 
i.setAdjustViewBounds(true); 
mRelativeLayout.addView(i, params); 

這對我的作品,並把我的圖標在屏幕的左上角具有指定保證金的兩側屏幕。 這有幫助嗎?

+0

真棒,我已經設置了我的ieye,但不能理解文件。 – 2010-07-21 15:35:10

+2

你很幸運,因爲我和你一樣,今天早上明白了。所以我很高興分享我的理解 – Sephy 2010-07-21 15:41:14

+0

像alignLeft等東西呢?似乎無法讓它識別它,而且似乎我所有的圖片都是從最後一行開始的。 – 2010-07-21 18:20:39