Android 相对年轻的子视图围绕在中心的可移动锚点上
我希望将relativelayout子视图与放置在中心的锚定视图一起调整。 所以基本上我有四个视图,一个角定位到父边,另一个角定位到中心视图。我过去已经成功地完成了这项工作,但这次锚定viewcenter视图是基于触摸移动动态定位的 预期结果是,子视图应根据定位视图位置调整自身大小。 取而代之的是,只有一个子视图是可见的,其大小按预期调整,而其他子视图则根本不可见 以下是我的布局:Android 相对年轻的子视图围绕在中心的可移动锚点上,android,android-relativelayout,Android,Android Relativelayout,我希望将relativelayout子视图与放置在中心的锚定视图一起调整。 所以基本上我有四个视图,一个角定位到父边,另一个角定位到中心视图。我过去已经成功地完成了这项工作,但这次锚定viewcenter视图是基于触摸移动动态定位的 预期结果是,子视图应根据定位视图位置调整自身大小。 取而代之的是,只有一个子视图是可见的,其大小按预期调整,而其他子视图则根本不可见 以下是我的布局: <RelativeLayout xmlns:android="http://schemas.android.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/viewBottom"
android:background="#ffff00"
android:text="test"
android:layout_below="@+id/dragView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/dragView"
android:layout_width="100dp"
android:layout_height="100dp" />
<View
android:id="@+id/viewTop"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/dragView"
android:layout_above="@+id/dragView"
android:background="#00ff00" />
<Button
android:id="@+id/dragView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#0000ff" />
Ontouch代码以移动定位视图
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
view.setLayoutParams(layoutParams);
break;
}
return true;
}
非常感谢您的任何帮助。
谢谢
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
view.setLayoutParams(layoutParams);
break;
}
return true;
}