2011-10-08 12 views
20

私はiOSから来ています。私はAndroidでequivを把握しようとしています。私はクリック時にビュー階層内のいくつかのスポットの上にイメージビューを移動したい。次のようなものがあります。AndroidのビューをReparent - あるレイアウトから別のレイアウトに移動

public boolean onTouchEvent(MotionEvent event) { 

    int action = event.getAction(); 

    //Tile touchBegan 
    if(action == MotionEvent.ACTION_DOWN) { 
     RelativeLayout rl = (RelativeLayout)(getParent().getParent()); 
     rl.addView(this); 
    } 
} 

これはもちろん、単純化された例ですが、別のレイアウトの下でビューを再親和する方法を理解しようとしています。私は信じられないほど複雑なようだ周りの検索この例..私は私の古いiOSの経験を覚えていれば右のそれはそれは昔の親はあなたが別のものにそれを追加した場合、自動的だからビューを削除し

http://blahti.wordpress.com/2011/02/10/moving-views-part-3/

+0

:あなたは同じ位置を保持する必要がある場合、それは私のコード例は、役立ちます以下答えることが可能です。 – Fattie

答えて

31

(私が間違っている可能性がありますを発見しました;-)。ここで

は、あなたがAndroid上で同じことをするだろう方法は次のとおりです。ここにグーグルで、誰のために

ViewGroup parent = (ViewGroup) yourChildView.getParent();  

if (parent != null) { 
    // detach the child from parent or you get an exception if you try 
    // to add it to another one 
    parent.removeView(yourChildView); 
} 

// you might have to update the layout parameters on your child 
// for example if your child was attached to a RelativeLayout before 
// and you add it to a LinearLayout now 
// this seems very odd coming from iOS but iOS doesn't support layout 
// management, so... 
LinearLayout.LayoutParams params = new LinearLayout.LayoutP... 
yourChildView.setLayoutParams(params); 

yourNewParent.addView(yourChildView); 
+0

助けてくれてありがとう、これは今私のために働いています!あまりにも簡単だと思って、私はこれを試したと思った。私はRelativeLayoutのレイアウトタイプに親をキャストしていたと思います。おそらくそれが私の問題を引き起こしていました。 – Daniel

+1

わかりやすい説明私たちはiOSersを混乱させる!ありがとう! – Fattie

7
((ViewGroup)view.getParent()).removeView(view); 
newContainer.addView(view) 
関連する問題