2011-08-31 10 views
1

私は2つの異なるレイアウトで2 ImageViewを持って、一つImageViewは別のImageView上にあると私はRelativeLayout &両ImageViewサイズはラップがコンテンツであるしかし、問題は、私はその時ImageView2をクリックした場合Imageview2Imageview1に表示されているとを使用していますそのときImageView1をクリックするとImageview1が表示されます。私はbring-to-frontメソッドを使用していますが、これは機能しません。悪い英語のコミュニケーションのために申し訳ありません。私を助けてください。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

こんにちはnik、私は私の質問を編集し、コードを入れている。 –

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

また、レイアウトを変更します。イメージビューを1つの相対的なレイアウトにする。そうでなければ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

ontouchではなくonclicklistenersを設定します。 – Ronnie

1

RelativeLayoutの代わりにFrameLayoutを使用します。 imageviewのwidhtとheightをmatch_parentに設定します。私はそれが確かにうまくいくだろう。

関連する問題