如何获取视图的边距,移动到 运行 时间未知的位置
How to get the margins of a View, moved to a position which isn't known at run time
截至目前,我正在开发一个 Android 应用程序,它提供了一些培训师可以用来与学员交流的工具,其中一个工具是阵容编辑器,它由拖放功能组成旋转器*.
*有一个默认布局设置->足球阵容:4/4/2
如果用户按下 saveBtn,阵容将被保存为自定义对象。称为 lineup,它包含 2 个具有 x 和 y 坐标的数组。它在 Recyclerview 中得到 repsesented,其中包含一个名为 "Edit" 的按钮。通过按下编辑按钮,我想通过在默认编辑模式 activity.
中将那些 x 和 y 坐标设置为微调器来显示保存的阵容
我的问题是,程序总是 returns 在 .xml.
中设置的默认页边距
我用 .getLeft 和 .getBottom 试过了,你可以在这里看到:
public Lineup saveLineup(){
int[] x=new int[10];
int[] y=new int[10];
LayoutParams[] layouts=new LayoutParams[10];
for(int i=0; i<10; i++){
x[i]=positions[i].getLeft();//positions contains all the spinners used in the .xml
y[i]=positions[i].getBottom();
}
Lineup lineup=new Lineup(x,y);
return lineup;
}
为了拖放,我按如下方式覆盖了 OnTouch 方法:
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
view.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
break;
default:
return false;
}
return true;
}
代替 getLeft()
、getBottom()
方法,使用 getLocationOnScreen(int[] outLocation)
获取视图相对于屏幕的坐标。
int[] outLocation = new int[2];
position[i].getLocationOnScreen(outLocation);
int marginleft=outLocation[0];
int margintop=outLocation[1];
详情见此:https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])
截至目前,我正在开发一个 Android 应用程序,它提供了一些培训师可以用来与学员交流的工具,其中一个工具是阵容编辑器,它由拖放功能组成旋转器*.
*有一个默认布局设置->足球阵容:4/4/2
如果用户按下 saveBtn,阵容将被保存为自定义对象。称为 lineup,它包含 2 个具有 x 和 y 坐标的数组。它在 Recyclerview 中得到 repsesented,其中包含一个名为 "Edit" 的按钮。通过按下编辑按钮,我想通过在默认编辑模式 activity.
中将那些 x 和 y 坐标设置为微调器来显示保存的阵容我的问题是,程序总是 returns 在 .xml.
中设置的默认页边距我用 .getLeft 和 .getBottom 试过了,你可以在这里看到:
public Lineup saveLineup(){
int[] x=new int[10];
int[] y=new int[10];
LayoutParams[] layouts=new LayoutParams[10];
for(int i=0; i<10; i++){
x[i]=positions[i].getLeft();//positions contains all the spinners used in the .xml
y[i]=positions[i].getBottom();
}
Lineup lineup=new Lineup(x,y);
return lineup;
}
为了拖放,我按如下方式覆盖了 OnTouch 方法:
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
view.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
break;
default:
return false;
}
return true;
}
代替 getLeft()
、getBottom()
方法,使用 getLocationOnScreen(int[] outLocation)
获取视图相对于屏幕的坐标。
int[] outLocation = new int[2];
position[i].getLocationOnScreen(outLocation);
int marginleft=outLocation[0];
int margintop=outLocation[1];
详情见此:https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])