【发布时间】:2023-03-09 20:13:01
【问题描述】:
我需要在运行时将图像动态放置在另一个图像上,恰好位于背景图像的 x,y 处。
我有以下详细信息:
叠加图像属性(红色):x = 58, y =232, h=266, w=431
背景图片(黑色):match_parent, w=1024,h=768
这里 x,y 是叠加层的左上角。
注意:叠加图像的 x,y 是相对于背景图像而不是整个屏幕布局。当 ActionBar 隐藏时,背景图像可能会被拉伸。背景图像的 layout_width 和 layout_height 设置为 match_parent。并且 scaleType 是“中心”。
使用以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout root = (RelativeLayout) findViewById(R.id.rootlayout);
backIV = (ImageView) findViewById(R.id.imageView1);
Content = new ImageView(this);
Content.setImageDrawable(getResources().getDrawable(R.drawable.content_1));
params = new RelativeLayout.LayoutParams(431, 266);
rl.addView(Content, params);
root.addView(rl);
}
从上面的代码:
[我想要什么]
从关于 SO 的许多解决方案和建议中,没有一个对我有用。相信我,我不会不尝试就发布这个问题。
【问题讨论】:
-
在RelativeLayout中为红色矩形设置内边距
-
你没有设置你想要重叠的图像的布局参数的 x 和 y。你只指定了高度和宽度(图像的大小)而不是它的位置