Android 如何让ImageView在ImageView的触摸事件中显示在最前面?

Android 如何让ImageView在ImageView的触摸事件中显示在最前面?,android,android-imageview,Android,Android Imageview,我有两个不同布局的ImageView,一个ImageView位于另一个ImageView上,我使用的是RelativeLayout&两个ImageView的大小都是包裹内容,但问题是,如果我在ImageView2显示在Imageview1上时单击ImageView2,如果我在ImageView2显示Imageview1时单击Imageview1,我使用的是“带到前面”方法,但这不是工作对不起,英语沟通不好。请帮帮我 提前谢谢 下面是我的代码 Main.xml:- <?xml version

我有两个不同布局的ImageView,一个ImageView位于另一个ImageView上,我使用的是RelativeLayout&两个ImageView的大小都是包裹内容,但问题是,如果我在ImageView2显示在Imageview1上时单击ImageView2,如果我在ImageView2显示Imageview1时单击Imageview1,我使用的是“带到前面”方法,但这不是工作对不起,英语沟通不好。请帮帮我

提前谢谢

下面是我的代码

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>

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

 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;
        }
    });
还要更改布局。将ImageView放入单个relativelayout中。否则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>

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

 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;
        }
    });
还要更改布局。将ImageView放入单个relativelayout中。否则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>


使用FrameLayout而不是RelativeLayout。并将imageview的宽度和高度设置为与父对象匹配。我相信它会工作得很好。

使用FrameLayout而不是RelativeLayout。并将imageview的宽度和高度设置为与父对象匹配。我相信它会很好用。

你能用mspaint画一幅画来详细说明吗?所以我们可以更好地理解和解决Hello nik,我已经编辑了我的问题并输入了代码。你到底想做什么?你还不清楚你想要什么,你想做的永远不会成功。如何在另一个视图下的视图上获得单击事件?如果在ImageView1返回到Imageview2时单击Imageview2,并且如果在Imageview2到达Imageview2前面时单击ImageView1,您能详细说明在mspaint中绘制图片吗?所以我们可以更好地理解和解决Hello nik,我已经编辑了我的问题并输入了代码。你到底想做什么?你还不清楚你想要什么,你想做的永远不会成功。如何在另一个视图下的视图上获得单击事件?如果在ImageView1返回到Imageview2时单击Imageview2,如果在Imageview2出现在Imageview2前面时单击ImageView1。我使用了此代码,但如果您有任何其他答案或链接,则此代码不起作用。我想您是这样的非常接近这个代码。试试看。设置onclicklisteners而不是ontouch。我尝试了你的代码和SetOnClickListener方法,但这不起作用。我使用了这段代码,但这段代码不起作用。如果你有任何其他答案或链接,请给我。我想你用这段代码非常接近它。试试看。设置onclicklisteners而不是ontouch。我尝试了你的代码和setonclicklistner方法,但这不起作用。