防止将ImageView中的图像拖出RelativeLayout时调整其大小

防止将ImageView中的图像拖出RelativeLayout时调整其大小,image,resize,imageview,Image,Resize,Imageview,我没有在其他主题中找到我问题的答案 我有一个背景可绘制的ImageView。此ImageView上有一个onTouchListener,可以将其拖动。现在我想向该视图添加一个ImageResource(连同一个填充)。图像应该总是在(方块)图像视图的中间。现在的问题是:如果我将整个ImageView拖出显示器,ImageView中的图像开始收缩/调整大小。当我把它拖回窗口时,它又开始增长到允许的大小(因为有一个填充) 如何防止将此图像拖出窗口时调整其大小 RelativeLayout layou

我没有在其他主题中找到我问题的答案

我有一个背景可绘制的ImageView。此ImageView上有一个onTouchListener,可以将其拖动。现在我想向该视图添加一个ImageResource(连同一个填充)。图像应该总是在(方块)图像视图的中间。现在的问题是:如果我将整个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);

如果此相对布局在布局的宽度或高度上具有WrapContent属性,则这些属性将动态更改

添加视图时,您正在修改其内容,依此类推这些属性

您需要像这样设置此imageView的布局属性。必须在布局参数的属性中设置要设置的位置和大小:

 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);
请记住,如果要在相对布局上添加某些内容,则这些属性是必需的

相对布局的设计是为了使内容适应固定的高度和宽度,所以请尝试在布局中修复这些属性

希望确实有用