通过将页边距设置为零,图像将填满整个屏幕。但是要将边距设置为一百,除右边之外,它都适合。为什么呢
ImageView imagen = new ImageView(this);
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relative.setLayoutParams(labelLayoutParams);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutImagenClicker = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layoutImagenClicker.setMargins(0, 0, 0, 0);
imagen.setBackgroundResource(R.drawable.barra_seek_bar);
imagen.setLayoutParams(layoutImagenClicker);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(imagen);
relative.addView(rl);
setContentView(relative);
ImageView imagen = new ImageView(this);
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relative.setLayoutParams(labelLayoutParams);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutImagenClicker = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
layoutImagenClicker.setMargins(100, 100, 100, 100);
imagen.setBackgroundResource(R.drawable.barra_seek_bar);
imagen.setLayoutParams(layoutImagenClicker);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(imagen);
relative.addView(rl);
setContentView(relative);
具有相同填充的不同边距?
layoutImagenClicker.setMargins(0, 0, 0, 0);
layoutImagenClicker.setMargins(100, 100, 100, 100);
看这个!
layoutImagenClicker.setMargins(100, 100, 200, 100);
相同的结果集保证金正确为100或200。这是一个错误吗?
最佳答案
尝试在RelativeLayout包装器上设置填充,然后删除imageview上的边距。至于您最初的问题,我不知道为什么会搞砸,但是我总是更喜欢从边距转移到填充...
ImageView imagen = new ImageView(this);
RelativeLayout relative = new RelativeLayout(this);
RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams
(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
// Padding on the RelativeLayout
relative.setPadding(100,100,100,100);
relative.setLayoutParams(labelLayoutParams);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutImagenClicker = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
// Remove the margin on the child
//layoutImagenClicker.setMargins(100, 100, 100, 100);
imagen.setBackgroundResource(R.drawable.barra_seek_bar);
imagen.setLayoutParams(layoutImagenClicker);
rl.setLayoutParams(new LayoutParams (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
rl.addView(imagen);
relative.addView(rl);
setContentView(relative);
祝好运