我在两个不同的布局中有两个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>