我怎么可以拖放的Andr​​oid RelativeLayout的内观 [英] How can I drag and drop Views within Android RelativeLayout

查看:190
本文介绍了我怎么可以拖放的Andr​​oid RelativeLayout的内观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做我自己的拖放功能。

I made my own drag and drop functionality.

我想用一个 RelativeLayout的做到这一点,但是这似乎没有很好地工作。 在开始的时候,我可以告诉每个查看我要拖上哪个位置应该通过设置它下面的查看之前。

I wanted to do this with a RelativeLayout, but this seems not to work very well. At the beginning i can tell each View I want to drag on which position it should be by setting it below the View before.

但是,当我想拖动一个视图,所有它下面的其他视图被移动过,因为他们希望保持我在其中设置它们的顺序。

But when I want to drag a View, all the other Views beneath it are moved too, because they want to keep the order in which I set them.

不过,我可以用这个还有什么其他的布局?我必须用这种阻力,并在不同的屏幕尺寸拖放功能,所以我不能'给他们固定 X 值。

But what other layout can I use for this? I have to use this drag and drop functionality on different screen sizes, so I cant' give them fixed x and y values.

推荐答案

我前一段时间面临着同样的麻烦,如果你不想拖累其他视图(低于或高于或视图旁边)与该视图拖,然后用 ViewGroup.LayoutParams ,而不是 RelativeLayout.LayoutParams ,后者是进口的,如果你按Ctrl-按住Shift 0,所以手动将其更改为前者。

I faced the same trouble a while ago, if you do not want to drag other views (either below or above or beside your view) along with the view being dragged, then use ViewGroup.LayoutParams instead of RelativeLayout.LayoutParams, the latter is imported if you hit Ctrl-Shift-O, so change it manually to the former.

编辑:既然你想让我怎么实现的是,这里是实际code

Since you wanted the description of how I achieved that, here is the actual code

 MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());              
 int left = (int) event.getRawX() - (v.getWidth() / 2);
 int top = (int) event.getRawY() - (v.getHeight());
 marginParams.setMargins(left, top, 0, 0);
 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
 image.setLayoutParams(layoutParams);  

我不得不做大量的研究来实现相同的,我花了2天弄清楚,一个叫MarginLayoutParams东西存在......

I had to do a lot of research to achieve the same and it took me 2 days to figure out that a thing called MarginLayoutParams exists...

请确保您有

import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;

为您的进口,而不是 RelativeLayout.LayoutParams

另外,还要确保你投marginparams您的相对布局 祝您好运...

Also make sure that you cast marginparams to your relative layout Best of luck...

这篇关于我怎么可以拖放的Andr​​oid RelativeLayout的内观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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