以绝对布局拖放问题 [英] Drag and drop problems in absolute layout

查看:85
本文介绍了以绝对布局拖放问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在相对布局中面临一个相当奇怪的问题



目标:

在布局中拖放文本视图(我使用了相对布局)

文本视图不得超出图像的尺寸



问题:

1)我尝试了几种方法来限制图像大小的文本视图,但没有成功

2)在某些地方拖动文本视图时的视图,它被剪掉或调用它消失(我不知道发生了什么)





< b>这是我的主要活动(DragActivity)

Hi All,

I am facing a rather weird problem in Relative layout

Objective:
Drag and drop text view in a layout(I have used relative layout)
The text view must not go beyond the dimensions of the image

Problem:
1)I tried several methods to constraint text view within image size, yet not successful
2)At some places in the view when the text view is dragged, it gets clipped off or call it disappears (I have no clue whats happening)


Here is my main activity(DragActivity)

package com.example.myapp8draganddrop;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class DragActivity extends Activity implements OnTouchListener  {

	TextView tv = null;
	RelativeLayout mRelativeLayoutText;
	boolean TOUCH_STATUS=false;
	int mLayoutLeft,mLayoutTop,mLayoutRight,mLayoutBottom; 
	int mLayoutWidth,mLayoutHeight;
	
	
	
	
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.activity_drag);
	 
	 mRelativeLayoutText = (RelativeLayout)findViewById(R.id.relLayout);
	 getDimensions();
	
	tv = (TextView)findViewById(R.id.textView1);
	 tv.setTextColor(Color.GREEN);
	 
	 
	 
	 tv.setOnTouchListener(new OnTouchListener() {

		 public boolean onTouch(View v, MotionEvent event) {
			 TOUCH_STATUS=true;
			 return false;
		 }
	  });
	
	
	 mRelativeLayoutText.setOnTouchListener(new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
		
			int left=0,top=0,right=0,bottom=0;
				if(TOUCH_STATUS==true) // any event from down and move
				 {
				 LayoutParams lp = (RelativeLayout.LayoutParams)tv.getLayoutParams();
				 
				 left = (int)event.getX()-tv.getWidth();
				 //left = left > mRelativeLayoutText.getLeft() ? left : mRelativeLayoutText.getLeft();
				 top = (int)event.getY()-tv.getHeight();
				 //top  = top > mRelativeLayoutText.getTop()? top : mRelativeLayoutText.getTop();
				 
				 right = (int)event.getX()+tv.getWidth();
				 //right = right < mRelativeLayoutText.getRight() ? right : mRelativeLayoutText.getRight();
				 bottom = (int)event.getY()+tv.getHeight();
				 //bottom = bottom < mRelativeLayoutText.getBottom() ? bottom : mRelativeLayoutText.getBottom();
				 
				 
				 lp.setMargins(left,top,right,bottom);
				 tv.setLayoutParams(lp);
				 tv.setTextColor(Color.RED);
				 
				 }
				if(event.getAction()==MotionEvent.ACTION_UP){
				 TOUCH_STATUS=false;
				 tv.setTextColor(Color.GREEN);
				 String desc = "Left:"+left+"\tTop:"+top+"\nRight"+right+"\tBottom:"+bottom;
				 Toast.makeText(getApplicationContext(), desc , Toast.LENGTH_SHORT).show();
				 }
			return true;
		 	}
	 	});
	 }	//End onCreate()

	private void getDimensions() {
		
		mLayoutWidth = mRelativeLayoutText.getWidth();
		mLayoutHeight = mRelativeLayoutText.getHeight();
		
		mLayoutLeft = mRelativeLayoutText.getRight() - mLayoutWidth;
		mLayoutTop = mRelativeLayoutText.getBottom() - mLayoutHeight;
		
		mLayoutRight = mRelativeLayoutText.getLeft() + mLayoutWidth;
		mLayoutBottom = mRelativeLayoutText.getTop() + mLayoutHeight;
		
	}

	public boolean onTouch(View arg0, MotionEvent arg1) {

		return false;
	}
}









< b>这是我的Layout.xml







Here is my Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"	

    android:layout_height="fill_parent" 	

    android:id="@+id/dragActivityLayout" >
      
  		 <RelativeLayout

			 android:id="@+id/relLayout"

			 android:layout_width="400dip"

			 android:layout_height="200dip"

			 android:removed="@drawable/dark_knight_rises"

			 > 
			 <TextView

				     android:id="@+id/textView1"

				     android:layout_width="wrap_content"

			     	 android:layout_height="wrap_content"

			     	 android:layout_alignParentLeft="true"

			     	 android:layout_alignParentTop="true"

			     	 android:singleLine="true"

			      	 android:text="@string/textView1"/>
		 </RelativeLayout> 

</RelativeLayout>







拖放屏幕截图

推荐答案

替换为此代码,您将获得正确的输出...这只适用于绝对布局



Replace with this code you will get correct output...and this will be only for absolute layout

<pre lang="java">
if(TOUCH_STATUS==true) // any event from down and move
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
img.setLayoutParams(lp);

}


这篇关于以绝对布局拖放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆