简体   繁体   English

具有XML布局的Android拖放

[英]Android Drag&Drop with XML Layout

The idea was to create an interface with two "buttons" that are draggable . 这个想法是创建一个带有两个可拖动的 “按钮”的界面。

I didn't want to use Canvas (problems with the bitmap in the memory and redrawing everything, etc.), so I decided to use the XML Layout with ImageView as buttons. 我不想使用Canvas(内存中的位图有问题,并重绘了所有内容,等等),所以我决定将XML Layout与ImageView一起用作按钮。

One button is central _vertical | 一键位于中央 _vertical | left , the second one central (for example). left ,第二个中央 (例如)。 The first one works fine, as it should, but the second one has strange margins on the both sides (left and right) and button cannot be dragged. 第一个可以正常工作,但是第二个在左右两侧都有奇怪的页边距,无法拖动按钮。 I checked also other positions: 我还检查了其他职位:

  1. top left corner - correct 左上角-正确
  2. bottom left corner - move only possible in x-axis... 左下角-只能在x轴上移动...
  3. top/bottom/center right - move only possible in y-axis 上/下/右中-仅在y轴上移动

Of course the situation is the same for any button with those properties, not only for "the second one". 当然,具有这些属性的任何按钮的情况都是相同的,不仅对于“第二个”按钮。 The code is for every button the same. 每个按钮的代码都相同。 It's really strange, but I guess it is a problem of the XML Layout: 确实很奇怪,但是我想这是XML布局的问题:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="left"
        android:gravity="center_vertical|left" >
        <ImageView
            android:id="@+id/leftChar"
            android:src="@drawable/ukreska"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center">

        <ImageView
            android:id="@+id/rightChar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/uotwarte" />
    </LinearLayout>
</RelativeLayout>

The position of the buttons are set with the Layout margin: 按钮的位置由布局边距设置:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(X, Y, 0, 0);
img.setLayoutParams(lp);

The position is calculated with event.getX() and event.getY() 使用event.getX()event.getY()计算位置

The version of the Android SDK 2.2. Android SDK 2.2的版本。 I've checked on the emulator and on the phone - the same results. 我已经在模拟器和电话上进行了检查-相同的结果。

Do you have any idea? 你有什么主意吗? Thanks in advance : ) 提前致谢 : )

Found the mistake. 发现了错误。 In case someone has the same problem: 如果有人有同样的问题:

When using XML layout and drag&drop, make sure you set proper XML gravity property. 使用XML布局和拖放时,请确保设置正确的XML 重力属性。 I removed it completely and I decided to set margins on the beginning of the Activity. 我将其完全删除,并决定在活动开始时设置边距。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM