Android 如何仅在特定的相对布局中移动edittext或imageview?
我已经编写了移动edittext和imageview的代码。虽然移动很好,但它也在相对布局之外移动。我必须限制仅在特定布局中移动。有人有建议吗?查看我在下面实现的拖放功能代码: dragandrop.javaAndroid 如何仅在特定的相对布局中移动edittext或imageview?,android,android-layout,touch-event,ontouchlistener,motionevent,Android,Android Layout,Touch Event,Ontouchlistener,Motionevent,我已经编写了移动edittext和imageview的代码。虽然移动很好,但它也在相对布局之外移动。我必须限制仅在特定布局中移动。有人有建议吗?查看我在下面实现的拖放功能代码: dragandrop.java public class MyActivity extends Activity implements View.OnTouchListener { TextView _view; ViewGroup _root; private int _xDelta;
public class MyActivity extends Activity implements View.OnTouchListener {
TextView _view;
ViewGroup _root;
private int _xDelta;
private int _yDelta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_root = (ViewGroup)findViewById(R.id.root);
_view = new TextView(this);
_view.setText("TextView!!!!!!!!");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50);
layoutParams.leftMargin = 50;
layoutParams.topMargin = 50;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
_view.setLayoutParams(layoutParams);
_view.setOnTouchListener(this);
_root.addView(_view);
}
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;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}}
公共类DragAndDrop扩展活动
{
private ImageView m_ivImage,m_ivImage1;
专用int m_计数器=0;
浮点m_lastTouchX、m_lastTouchY、m_posX、m_posY、m_prevX、m_prevY、m_imgXB、m_imgXC、m_imgYC、m_dx、m_dy;
私人线路布置图;
私有绝对布局m_alTop;
专用按钮m_btnAddView,m_btnRemove;
私有语境m_语境;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_context=这个;
m_prevX=0;
m_prevY=0;
m_imgXB=50;
m_imgYB=100;
m_imgXC=150;
m_imgYC=100;
m_ivImage=(ImageView)findViewById(R.id.ivImage);
m_ivImage1=(ImageView)findViewById(R.id.ivImage1);
m_llTop=(线性布局)findViewById(R.id.llTop);
m_alTop=(绝对布局)findViewById(R.id.alTop);
m_btnAddView=(按钮)findviewbyd(R.id.btnAdd);
m_btnRemove=(按钮)findViewById(R.id.btnRemove);
m_ivImage.setOnTouchListener(m_onTouchListener);
m_ivImage1.setOnTouchListener(m_onTouchListener);
m_btnAddView.setOnClickListener(m_onClickListener);
m_btnRemove.setOnClickListener(m_onClickListener);
}
/**
*可单击控件的公共单击侦听器
*/
OnClickListener m_OnClickListener=新的OnClickListener(){
@凌驾
单击公共void onClick(视图p_v)
{
开关(p_v.getId())
{
案例R.id.btnAdd:
addView();
打破
案例R.id.BTN删除:
removeView();
打破
违约:
打破
}
}
};
/**
*触摸监听器查看
*/
OnTouchListener m_OnTouchListener=新建OnTouchListener(){
@凌驾
公共布尔onTouch(查看p_v、运动事件p_事件)
{
开关(p_event.getAction())
{
case MotionEvent.ACTION\u DOWN:
{
m_lastTouchX=p_event.getX();
m_lastTouchY=p_event.getY();
打破
}
case MotionEvent.ACTION\u UP:
{
打破
}
case MotionEvent.ACTION\u移动:
{
m_dx=p_event.getX()-m_lastTouchX;
m_dy=p_event.getY()-m_lastTouchY;
m_posX=m_prevX+m_dx;
m_posY=m_prevY+m_dy;
如果(m_posX>0&&m_posY>0&&(m_posX+p_v.getWidth())
布局文件
public class DragAndDrop extends Activity
{
private ImageView m_ivImage, m_ivImage1;
private int m_counter = 0;
float m_lastTouchX, m_lastTouchY, m_posX, m_posY, m_prevX, m_prevY, m_imgXB, m_imgYB, m_imgXC, m_imgYC, m_dx, m_dy;
private LinearLayout m_llTop;
private AbsoluteLayout m_alTop;
private Button m_btnAddView, m_btnRemove;
private Context m_context;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_context = this;
m_prevX = 0;
m_prevY = 0;
m_imgXB = 50;
m_imgYB = 100;
m_imgXC = 150;
m_imgYC = 100;
m_ivImage = (ImageView) findViewById(R.id.ivImage);
m_ivImage1 = (ImageView) findViewById(R.id.ivImage1);
m_llTop = (LinearLayout) findViewById(R.id.llTop);
m_alTop = (AbsoluteLayout) findViewById(R.id.alTop);
m_btnAddView = (Button) findViewById(R.id.btnAdd);
m_btnRemove = (Button) findViewById(R.id.btnRemove);
m_ivImage.setOnTouchListener(m_onTouchListener);
m_ivImage1.setOnTouchListener(m_onTouchListener);
m_btnAddView.setOnClickListener(m_onClickListener);
m_btnRemove.setOnClickListener(m_onClickListener);
}
/**
* Common click listener for clickable controls
*/
OnClickListener m_onClickListener = new OnClickListener(){
@Override
public void onClick(View p_v)
{
switch (p_v.getId())
{
case R.id.btnAdd:
addView();
break;
case R.id.btnRemove:
removeView();
break;
default:
break;
}
}
};
/**
* Touch listener for view
*/
OnTouchListener m_onTouchListener = new OnTouchListener(){
@Override
public boolean onTouch(View p_v, MotionEvent p_event)
{
switch (p_event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
m_lastTouchX = p_event.getX();
m_lastTouchY = p_event.getY();
break;
}
case MotionEvent.ACTION_UP:
{
break;
}
case MotionEvent.ACTION_MOVE:
{
m_dx = p_event.getX() - m_lastTouchX;
m_dy = p_event.getY() - m_lastTouchY;
m_posX = m_prevX + m_dx;
m_posY = m_prevY + m_dy;
if (m_posX > 0 && m_posY > 0 && (m_posX + p_v.getWidth()) < m_alTop.getWidth() && (m_posY + p_v.getHeight()) < m_alTop.getHeight())
{
p_v.setLayoutParams(new AbsoluteLayout.LayoutParams(p_v.getMeasuredWidth(), p_v.getMeasuredHeight(), (int) m_posX, (int) m_posY));
m_prevX = m_posX;
m_prevY = m_posY;
}
break;
}
}
return true;
}
};
/**
* Add view dynamically for drag and drop
*/
private void addView()
{
ImageView m_img = new ImageView(m_context);
if (m_counter < 5)
{
m_img.setBackgroundResource(R.drawable.ic_launcher);
m_alTop.addView(m_tv, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
m_alTop.addView(m_img, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, ((int) m_imgXB), ((int) m_imgYB)));
m_counter++;
if (m_counter == 5)
m_btnAddView.setEnabled(false);
}
m_img.setOnTouchListener(m_onTouchListener);
m_tv.setOnTouchListener(m_onTouchListener);
}
public void removeView()
{
m_counter = 0;
m_alTop.removeAllViews();
m_alTop.invalidate();
m_btnAddView.setEnabled(true);
}
尝试使用绝对布局。@GrIsHu,我尝试使用绝对布局,但它没有设置边距。谢谢回复,但它对我不起作用。当我使用绝对布局时,该视图没有移动。它保持静止。我只需要在动作视图中布局的当前高度和宽度,就像您在if条件中定义的那样。这可能吗?是的,您可以获得布局的当前高度和宽度我在代码中定义并限制了高度和宽度。您可能错过了视图的触摸侦听器来移动它。不。我不能仅在线性布局中限制它。第二个问题是,当我移动最新的edittext或imageview时,它也移动最旧的edittext或imageview。我在Google+中添加了您。有解决方案吗?您尝试过我的代码吗?它工作得很好。只要试一下我的代码。好的。除了这个问题。我们可以存储布局和内部布局的高度、宽度、X轴和Y轴吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llTop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AbsoluteLayout
android:id="@+id/alTop"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:layout_weight=".70"
>
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:src="@drawable/ic_menu_share"
android:visibility="gone" />
<ImageView
android:id="@+id/ivImage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_x="193dp"
android:layout_y="29dp"
android:src="@drawable/ic_launcher" />
</AbsoluteLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight=".30" >
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/addview" />
<Button
android:id="@+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/removeview"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>