如何在Android应用程序中将一张图片放置在另一张图片上方?

4
我该如何将im2放在正确的位置?
FrameLayout rv =(FrameLayout)findViewById(R.id.my_ph);


    ImageView im1 = new ImageView(this);
    im1.setBackgroundResource(R.drawable.lamp_on);
    im1.layout(100, 100,120, 120);

    rv.addView(im1);

我的布局

<FrameLayout android:id="@+id/my_ph" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView  
android:id="@+id/image"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/sketch" />
</FrameLayout>

我希望IM1能够在图片视图的位置x,y上方。
4个回答

12

在布局中再添加一个ImageView,并将其上下左右对齐到第一个ImageView。将其设置为不可见。

<FrameLayout android:id="@+id/my_ph"    
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView  
    android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/sketch" android:layout_alignParentTop="true"/>
<ImageView  
    android:id="@+id/image2"
    android:layout_width="fill_parent" 
    android:layout_height="fill_paren" 
    android:layout_alignTop="@id/image"
    android:layout_alignLeft="@id/image"
    android:layout_alignRight="@id/image"
    android:layout_alignBottomp="@id/image"
    android:visibility="INVISIBLE"/>
</FrameLayout>

然后在代码中:

ImageView image2 =(ImageView)findViewById(R.id.image2);
image2.setVisibility(View.VISIBLE);
image2.setImageResource(R.drawable.my_image);

2

并且答案是...

 RelativeLayout rv = (RelativeLayout) findViewById(R.id.my_ph);
 RelativeLayout.LayoutParams params;
 ImageButton im1 = new ImageButton(this);

 im1.setBackgroundResource(R.drawable.lamp);
 im1.setId(i);
 im1.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
        TextView tx = (TextView) findViewById(R.id.textView1);
        tx.setText("lamp #" + v.getId());
     }
 });

 params = new RelativeLayout.LayoutParams(40, 40);
 params.leftMargin = x;
 params.topMargin = y;
 rv.addView(im1, params);

XML布局:

 <RelativeLayout 
            android:id="@+id/my_ph"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:gravity="bottom">
    <ImageView 
            android:id="@+id/image" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true"
            android:background="@drawable/map" />
    <TextView 
            android:id="@+id/textView1" 
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_below="@+id/image" 
            android:layout_alignParentLeft="true">
    </TextView>

 </RelativeLayout>

1

在imageView的xml中,不要使用android:src,而是使用android:background,然后在源java文件中接收它,然后使用以下代码:

ImageView img=(ImageView)findViewById(R.id.image);
img.setBackgroundDrawable(getResource().getDrawable(R.drawable.lamp_on));

希望这对你有用。


问题是关于第二张图片的位置(x,y)。 - eyalb

0

在您的drawable文件夹中使用此代码制作xml,您可以以相同的方式添加更多图像。它们将全部叠放在一起。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item>
    <bitmap android:src="@drawable/img1"
        android:gravity="center"
        />
</item>
<item>
    <bitmap android:src="@drawable/img2"
        android:gravity="center"
        />
</item>
</layer-list>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接