2014-03-13 125 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垂直居中对齐。)