2014-03-13 44 views
0

很可能有一個重複的問題,但我還沒有找到它。我正在做所有的事情,而不是使用xml。基本上我想要做的是在圖像下面顯示EditText。我正在使用帶有ImageView和EditText的RelativeLayout。Android ImageView佈局問題

這些是我設立了ImageView的和的EditText參數:

RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
editTextParams.width=500; 
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
imageParams.addRule(RelativeLayout.ABOVE, editText.getId()); 

我已經驗證正確地放置在的EditText右下角和上面的圖片。我碰到的是如果圖片「太高」,那麼它覆蓋EditText。我也試過使用

imageView.setScaleType(ImageView.ScaleType.FIT_XY); 

它也延伸到EditText。

,我使用完整的代碼是這樣的

RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout); 
ImageView imageView = new ImageView(this); 
EditText editText = new EditText(this); 

RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
editTextParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
editTextParams.width=500; 
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
imageParams.addRule(RelativeLayout.ABOVE, editText.getId()); 

imageView.setLayoutParams(imageParams); 
imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
editText.setLayoutParams(editTextParams); 
Bitmap image = getImage(); 
imageView.setImageBitmap(image); 
layout.addView(editText); 
layout.addView(imageView); 

感謝。任何建議將不勝感激。

enter image description here

+0

500寬度是很多 – Murad

回答

1

創建的LinearLayout,然後添加在它的兩個組件(ImageView的和EditText上)

這裏是你應該做的;

  • 設置方向垂直的水平佈局和寬度,無論你需要
  • 設置爲與組件的爲0,權重爲1的每個

之後,你應該有兩個項目一個在另一個之上;

我希望這有助於

+0

這是最有幫助的(對不起,其他人),並解決了我的原始問題。 EditText現在總是出現在圖片下方。我現在遇到了不同圖片的問題(我猜根據圖片的尺寸)會讓我在圖片的實際邊框和EditText之間留下很大的空白區域,但是您已經很好地瞭解了我辦法。謝謝:) – jhnewkirk

+0

我很高興它幫助你! – Eenvincible

0

一兩件事你可以做的是修復ImageView的寬度和高度。這樣,您可以控制最大尺寸,而無需通過編輯文本進行成像。

RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256); 

希望這會有所幫助。

+0

我試着這但它並沒有解決這個問題。它所做的只是使圖片更小,但沒有解決問題。看到上面的屏幕截圖,這是原始的,圖片仍然像這樣重疊,但只是縮小。 – jhnewkirk

+0

hmm。另一個要嘗試的是,不要在edittext上面添加圖片,而是嘗試在圖片下方添加edittext。所以將LayoutParams更改爲下面的LayoutParams。而不是'imageParams.addRule(RelativeLayout.ABOVE,editText.getId());'使用這個 'editTextParams.addRule(RelativeLayout.BELOW,imageView.getId());'首先將圖像添加到視圖然後edittext 。 – Parnit

0

您的editText有一個垂直的MATCH_PARENT。它不應該是WRAP_CONTENT嗎?

(我不知道這會有所幫助,但它可能會幫助,因爲MATCH_PARENT違揹你的計劃的佈局,和你的截圖表明EDITTEXT垂直居中對齊。)