Android——RelativeLayout中在任意位置动态添加图片

方法很简单,直接上代码
		lay1 = (RelativeLayout) findViewById(R.id.lay1);
		Drawable drawable = getDrawable(R.mipmap.ic_launcher_round) ;//这里我用的Android自带的图片
		ImageView imageView = new ImageView(this);
        imageView.setImageDrawable(drawable);
        imageView.setX(800);//设定图片的开始位置的横坐标,Y同理
        lay1.addView(imageView);

我在自己做一个项目,需要一开始先加载几个相同的图片放在不同位置,因为具体数量不一定所以需要动态添加,坐标存在数组中,所以在思考这个问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android RelativeLayout 添加 Button 组有多种方法。以下是其的一些方法: 1.使用 XML: 在 RelativeLayout 添加 Button 组,可以在 XML 文件使用以下代码: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_below="@id/button1" /> </RelativeLayout> ``` 在这个例子,我们创建了两个 Button,一个在另一个下方。 2.使用 Java 代码: 在 RelativeLayout 添加 Button 组,可以使用以下代码: ``` RelativeLayout relativeLayout = new RelativeLayout(this); Button button1 = new Button(this); button1.setText("Button 1"); Button button2 = new Button(this); button2.setText("Button 2"); RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); button1.setLayoutParams(params1); RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params2.addRule(RelativeLayout.BELOW, button1.getId()); params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); button2.setLayoutParams(params2); relativeLayout.addView(button1); relativeLayout.addView(button2); ``` 在这个例子,我们创建了两个 Button,并设置它们的位置和大小。 以上是在 Android RelativeLayout 添加 Button 组的两种方法,你可以根据你的需求选择其一种。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值