很可能是一个重复的问题,但我还没有找到它。我正在以编程方式执行所有操作,而不是使用xml。基本上我要做的是让EditText出现在图像下方。我使用RelativeLayout与ImageView和EditText。
这些是我为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);
感谢。任何建议都将不胜感激。
答案 0 :(得分:1)
创建一个LinearLayout,然后在其中添加两个组件(ImageVIew和EditText)
这是你应该做的;
之后,你应该将这两个项目放在另一个之上;
我希望这会有所帮助
答案 1 :(得分:0)
您可以做的一件事是修复imageview的宽度和高度。这样,您可以控制最大尺寸,而不会使图像覆盖编辑文本。
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(256, 256);
希望这有帮助。
答案 2 :(得分:0)
您的editText有一个垂直MATCH_PARENT。不应该是WRAP_CONTENT吗?
(我不确定这是否有帮助,但它可能会有所帮助,因为MATCH_PARENT与您计划的布局相矛盾,并且您的屏幕截图表明editText是垂直居中的。)