android绝对定位图片最上层,设置视图的绝对位置

您可以使用RelativeLayout。假设您希望在布局中的位置(50,60)有一个30x40的ImageView。在你活动的某个地方:

// Some existing RelativeLayout from your layout xml

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);

ImageView iv = new ImageView(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);

params.leftMargin = 50;

params.topMargin = 60;

rl.addView(iv, params);

更多的例子:

分别在(50,60)和(80,90)处放置两个30x40 ImageViews(一个黄色,一个红色):

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);

ImageView iv;

RelativeLayout.LayoutParams params;

iv = new ImageView(this);

iv.setBackgroundColor(Color.YELLOW);

params = new RelativeLayout.LayoutParams(30, 40);

params.leftMargin = 50;

params.topMargin = 60;

rl.addView(iv, params);

iv = new ImageView(this);

iv.setBackgroundColor(Color.RED);

params = new RelativeLayout.LayoutParams(30, 40);

params.leftMargin = 80;

params.topMargin = 90;

rl.addView(iv, params);

放置一个30x40黄色图像视图(50,60)和另一个30x40红色图像视图<80,90>相对于黄色图像视图:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);

ImageView iv;

RelativeLayout.LayoutParams params;

int yellow_iv_id = 123; // Some arbitrary ID value.

iv = new ImageView(this);

iv.setId(yellow_iv_id);

iv.setBackgroundColor(Color.YELLOW);

params = new RelativeLayout.LayoutParams(30, 40);

params.leftMargin = 50;

params.topMargin = 60;

rl.addView(iv, params);

iv = new ImageView(this);

iv.setBackgroundColor(Color.RED);

params = new RelativeLayout.LayoutParams(30, 40);

params.leftMargin = 80;

params.topMargin = 90;

// This line defines how params.leftMargin and params.topMargin are interpreted.

// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.

params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);

rl.addView(iv, params);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值