我没有在其他的题目中找到我的问题的答案。
我有一个背景可绘制的ImageView。这个ImageView上面有一个onTouchListener,你可以拖拽它。现在我想在这个视图中添加一个ImageResource (连同一个填充)。图像应该总是在(正方形) ImageView的中间。现在的问题是:如果我将整个ImageView拖出显示器,ImageView中的图像开始收缩/调整大小。当我将它拖回到窗口中时,它又开始增长到允许的大小(因为有填充)。
如何防止此图像在拖出窗口时被调整大小?
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
ImageView i = new ImageView(this);
i.setPadding(10);
i.setImageResource(R.drawable.image);
i.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg));
i.setPositionAndSize(0, 0); // creates layout params and positions
// it with margins in relative layout
i.setOnTouchListener(touch);
layout.addView(i);
发布于 2013-05-10 11:43:46
如果此相对布局在layout_width或height上具有WrapContent属性,则这些属性将动态更改。
当您添加一个视图时,您正在修改它的内容,等等这些属性。
您需要将布局属性设置为该imageView,如下所示。您要设置的positionAndSize必须在布局参数的属性中设置:
RelativeLayout.LayoutParams lyp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView i = new ImageView(this);
(the properties you want the parent of the view (layout) to receive must be setted here in "lyp").
i.setLayoutParams(lyp);
layout.addView(i,lyp);
请记住,如果您在相对布局上添加某些内容,则这些属性是必需的。
相对布局的设计是为了使内容适合固定的高度和宽度,因此请尝试在布局中修复这些属性。
霍普确实帮了我!!
https://stackoverflow.com/questions/14876529
复制相似问题