2016-08-19 11 views
0

私は>>>□口□RelativeLayoutに余白のないビューを別のビューに揃える方法は?

<View 
    android:id="@+id/a" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:background="@color/opaque_red" /> 
<View 
    android:id="@+id/b" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_alignBottom="@id/a" 
    android:layout_toRightOf="@id/a" 
    android:background="@color/opaque_red" /> 
<View 
    android:id="@+id/c" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_alignBottom="@id/a" 
    android:layout_toLeftOf="@id/a" 
    android:background="@color/opaque_red" /> 

これらのビューを作成した。そして、私はaが画面内に

// v is whole screen 
a=v.findViewById(R.id.a); 

a.setOnTouchListener(new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 

     if (motionEvent.getAction() == MotionEvent.ACTION_MOVE || motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 

      int x = (int) motionEvent.getRawX(); 
      int y = (int) motionEvent.getRawY(); 

      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) a.getLayoutParams(); 

      // calculate x y should set to a, int[0] is x, int[1] is y 
      int[] xy=centerPointToLeftTop(x,y,a.getMeasuredWidth(),a.getMeasuredHeight()); 

      // limit the a inside the screen. b and c just follow the a, they can go to outside of screen 
      if(xy[0]<0) { 
       params.leftMargin = 0; 
      } else if (xy[0] > v.getMeasuredWidth()- a.getMeasuredWidth()){ 
       params.leftMargin=v.getMeasuredWidth()-a.getMeasuredWidth(); 
      } else { 
       params.leftMargin = xy[0]; 
      } 
      a.setLayoutParams(params); 
      v.invalidate(); 
     } 

     return true; 
    } 
}); 

を移動することができますマージンは、Android

にビューの位置を変更する唯一の方法であることを確認してください

しかし、余白は2つのビュー間のアライメントにも影響しますので、cビュー(左の四角)は表示されませんaビュー

マージンのないビューの整列方法は?またはマージンを変更せずにビューを移動する別の方法がありますか?

答えて

0

ビューCから

android:layout_alignBottom="@id/a" 
android:layout_toLeftOf="@id/a" 

を削除します。これらの行がアンドロイドに通知するので、動きがあればcも移動する必要があります。したがって、これらの行を削除して、ビューを要素aにではなくウィンドウに関連付ける属性を追加します。

+0

これらを削除すれば、 'a'の左に' c'を押して、 'a'を動かすと' a'に従う方法は? –

+0

スクリーンショットまたはレイアウトを追加できますか?私たちはあなたの問題を正確に理解することができます。 –

関連する問題