2011-08-31 68 views
1

我有兩個二ImageView不同的佈局中,一個ImageView是另一個ImageView,我使用RelativeLayout &兩個ImageView大小包裹的內容,但如果我上ImageView2單擊時間Imageview2顯示在Imageview1和問題如果我點擊ImageView1,那麼Imageview1會顯示在Imageview2上我正在使用引導式方法,但這不起作用。對不起英文交流。請幫幫我。如何獲取ImageView帶來ImageView的觸摸事件?

在此先感謝

以下是我的代碼。

的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; 
      } 
     }); 
    } 
} 
+1

你能詳細描述一下mspaint中的圖片嗎?所以我們可以更好地理解和解決 –

+0

你好,尼克,我編輯了我的問題,並把代碼。 –

+0

究竟你想做什麼?你還沒有清楚你想要什麼。 –

回答

4

您可以把點擊視圖上方的背景視圖。當您點擊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; 
     } 
    }); 

也改變佈局。將圖像放入單個相關佈局中。否則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> 
+0

我使用了這段代碼,但是如果你有任何其他的答案或鏈接,請告訴我這段代碼不工作。 –

+0

我覺得你用這段代碼非常接近它。試一試。 – Ronnie

+0

設置onclicklisteners而不是ontouch。 – Ronnie

1

使用FrameLayout而不是RelativeLayout。並將imageview的widht和height設置爲match_parent。我確定它會正常工作。