gpt4 book ai didi

android - 如何让ImageView Bring to front on touch event of ImageView?

转载 作者:行者123 更新时间:2023-11-29 15:30:37 24 4
gpt4 key购买 nike

我在两个不同的布局中有两个 ImageView,一个 ImageView 在另一个 ImageView 上,我正在使用 RelativeLayoutImageView 大小都是 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;
}
});
}
}

最佳答案

您可以将背景 View 置于点击 View 之上。当您点击 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 放入单个相对布局中。否则 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>

关于android - 如何让ImageView Bring to front on touch event of ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com