ImageView,如何设置图片出现在另一张图片的前面?

ImageView, How to set image seem at the front of another image?

我对图像有疑问。我怎样才能把一个图像放在另一个图像前面。 问题是我用代码创建图像。所以我无法更改布局中的顺序。

您可以使用 ImageView.bringToFront();,也许是最好的解决方案。

这是Tutorial

如果您有任何问题,请告诉我。

你可以使用 RelativeLayout:

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>

如果您通过代码创建图像,您可以使用 LayoutParams 为第二个图像提供负的 leftMargin。

    RelativeLayout.LayoutParams imageParams = new  RelativeLayout.LayoutParams
            (RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    imageParams.leftMargin = -20; // change this value 
    imageParams.addRule(RelativeLayout.RIGHT_OF,image1.getId());
    image2.setLayoutParams(imageParams);

这会强制第二张图片位于第一张图片之上。通过更改左边距,您可以决定您希望第二张图片在第一张图片上移动多少。