我在两个不同的布局中有两个ImageView,一个ImageView在另一个ImageView上,并且我正在使用RelativeLayout且两个ImageView的大小都是Wrap Content,但是问题是如果我单击ImageView2那时Imageview2显示在Imageview1上,并且如果我单击ImageView1那时Imageview1显示在Imageview2上,我正在使用前移方法,但是这种方法不起作用。对不起,英语沟通不好。请帮我。

提前致谢

以下是我的代码。

Main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:layout_width="fill_parent" android:id="@+id/mRlayout1"
        android:layout_height="fill_parent">
        <ImageView android:layout_width="wrap_content"
            android:scaleType="matrix" android:layout_height="wrap_content"
            android:id="@+id/mImageView1" android:src="@drawable/icon1" />
    </RelativeLayout>
    <RelativeLayout android:layout_width="fill_parent" android:id="@+id/mRlayout2"
        android:layout_height="fill_parent" >
        <ImageView android:layout_width="wrap_content"
            android:scaleType="matrix" android:layout_height="wrap_content"
            android:id="@+id/mImageView2" android:src="@drawable/icon" />
    </RelativeLayout>
</RelativeLayout>


Java档案:-

public class BodyTemp extends Activity {
    ImageView mImageView1, mImageView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mImageView1 = (ImageView) findViewById(R.id.mImageView1);
        mImageView1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                mImageView1.bringToFront();
                return true;
            }
        });
        mImageView2 = (ImageView) findViewById(R.id.mImageView2);
        mImageView2.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                mImageView2.bringToFront();
                return true;
            }
        });
    }
}

最佳答案

您可以将背景视图置于单击的视图上方。单击mImageView2时,将mImageView1放在最前面,反之亦然。

 mImageView2 = (ImageView) findViewById(R.id.mImageView2);
mImageView1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            mImageView2.bringToFront();
            return true;
        }
    });
    mImageView2.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            mImageView1.bringToFront();
            return true;
        }
    });


同时更改布局。将图像视图放入一个相对布局中。否则bringToFront将不起作用。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView android:layout_width="wrap_content"
        android:scaleType="matrix" android:layout_height="wrap_content"
        android:id="@+id/mImageView1" android:src="@drawable/icon1" />

    <ImageView android:layout_width="wrap_content"
        android:scaleType="matrix" android:layout_height="wrap_content"
        android:id="@+id/mImageView2" android:src="@drawable/icon" />
</RelativeLayout>

09-28 04:49