Android:允许将TextView拖到RelativeLayout之外
我已经创建了一个OnTouchListener,它允许我在屏幕上拖动视图。除了一个问题外,这工作正常: 我的视图将调整大小,而不是离开父视图。他们处于相对的状态,我怀疑这是造成问题的原因。它们仅在触摸我的RelativeLayout的右侧或底部边界时调整大小 我希望有一个简单的解决方案来解决这个问题(理想情况下不涉及调整RelativeLayout的大小),但我还没有找到解决方案 理想情况下,此解决方案将适用于所有视图,因为我将此OnTouchListener用于TextView和ImageView。我希望找到一个XML解决方案。我假设其他人已经处理过这个问题 附言:在我发布这篇文章之前,我刚刚记得我相信有一个标志可以打到RelativeLayout的边框,所以当出现这种情况时,也许我可以对TextView应用负边距。想法 编辑 以下是我的听众:Android:允许将TextView拖到RelativeLayout之外,android,drag-and-drop,textview,imageview,drag,Android,Drag And Drop,Textview,Imageview,Drag,我已经创建了一个OnTouchListener,它允许我在屏幕上拖动视图。除了一个问题外,这工作正常: 我的视图将调整大小,而不是离开父视图。他们处于相对的状态,我怀疑这是造成问题的原因。它们仅在触摸我的RelativeLayout的右侧或底部边界时调整大小 我希望有一个简单的解决方案来解决这个问题(理想情况下不涉及调整RelativeLayout的大小),但我还没有找到解决方案 理想情况下,此解决方案将适用于所有视图,因为我将此OnTouchListener用于TextView和ImageVi
// Dragging functionality for all views and double tap to remove
// functionality for ImageViews
final OnTouchListener onTouchListener = new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
v.bringToFront();
//if(v instanceof android.widget.ImageView)
//mScaleDetector.onTouchEvent(event);
layoutParams = (LayoutParams) v.getLayoutParams();
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
// Where the user started the drag
pressed_x = (int) event.getRawX();
pressed_y = (int) event.getRawY();
if (v instanceof android.widget.ImageView)
{
curTime = System.currentTimeMillis();
if(curTime - prevTime <= DOUBLE_TAP_INTERVAL)
{
v.setVisibility(View.GONE);
}
prevTime = curTime;
}
case MotionEvent.ACTION_MOVE:
// Where the user's finger is during the drag
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
// Calculate change in x and change in y
dx = x - pressed_x;
dy = y - pressed_y;
// Update the margins
layoutParams.leftMargin += dx;
layoutParams.topMargin += dy;
v.setLayoutParams(layoutParams);
// Save where the user's finger was for the next ACTION_MOVE
pressed_x = x;
pressed_y = y;
break;
default:
break;
}
return true;
}
}
//拖动所有视图的功能并双击以删除
//ImageView的功能
最终OnTouchListener OnTouchListener=新OnTouchListener()
{
@凌驾
公共布尔onTouch(视图v,运动事件)
{
v、 布林托夫隆();
//if(v instanceof android.widget.ImageView)
//mScaleDetector.onTouchEvent(事件);
layoutParams=(layoutParams)v.getLayoutParams();
开关(event.getActionMasked())
{
case MotionEvent.ACTION\u DOWN:
//用户开始拖动的位置
按下的_x=(int)event.getRawX();
按下的_y=(int)event.getRawY();
if(v instanceof android.widget.ImageView)
{
curTime=System.currentTimeMillis();
如果(curTime-prevTime您的问题代码在哪里?我尝试过这样做,不需要为任何内容设置任何负边距,您只需重置给定视图的左上角,它就会自动从屏幕上滑下。您可能可以发布activity.xml
和yourActivity.java
代码。这样就更容易取消设置我添加了我的OnTouchListener。它还有一些其他功能。