Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 移动拖放ImageButton时,ImageButtons被挤压_Java_Android_Mobile_Drag And Drop_Imagebutton - Fatal编程技术网

Java 移动拖放ImageButton时,ImageButtons被挤压

Java 移动拖放ImageButton时,ImageButtons被挤压,java,android,mobile,drag-and-drop,imagebutton,Java,Android,Mobile,Drag And Drop,Imagebutton,我设置了4个普通的imagebutton,然后是第5个imagebutton,它是一个拖放按钮。我希望4个非拖放的imagebutton保持在一个特定的位置,不管imagebutton是拖放的发生了什么。当我移动拖放imagebutton时,其他imagebutton也会移动并被压扁 你知道我如何在不影响其他imagebutton的情况下移动这个拖放imagebutton吗 以下是我的xml: <LinearLayout xmlns:android="http://schemas.andr

我设置了4个普通的imagebutton,然后是第5个imagebutton,它是一个拖放按钮。我希望4个非拖放的imagebutton保持在一个特定的位置,不管imagebutton是拖放的发生了什么。当我移动拖放imagebutton时,其他imagebutton也会移动并被压扁

你知道我如何在不影响其他imagebutton的情况下移动这个拖放imagebutton吗

以下是我的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gllayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/mars" >

<TextView
    android:id="@+id/scores"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageButton
    android:id="@+id/mainHut"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/mainhut" />

<ImageButton 
    android:id="@+id/polarCapButton1"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/polarcap" />

<ImageButton 
    android:id="@+id/polarCapButton2"
    android:layout_marginTop="-70dp"
    android:layout_marginLeft="575dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/polarcap" />

<ImageButton 
    android:id="@+id/spaceRockButton1"
    android:layout_marginTop="100dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/spacerock" />

<ImageButton 
    android:id="@+id/spaceRockButton2"
    android:layout_marginTop="-140dp"
    android:layout_marginLeft="520dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/spacerock" />

<ImageButton
    android:id="@+id/meteor"
    android:visibility="invisible"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/meteor" 
    android:layout_marginTop="-100dp"
    android:layout_marginLeft="400dp" />

</LinearLayout>

提前感谢您的帮助。

在不查看XML文件的情况下,这可能是由于使用了相对布局-4个静态按钮可能相对于正在移动的按钮定位,并且在拖放时将实时移动。尝试使用绝对定位(例如,
android:layout_marginTop=“30dp”
)而不是相对定位,或者使用其他类型的布局。如果您可以将您的活动和布局的XML文件发布到发生这种情况的地方,这将更好地帮助您提供一些信息,以获得更合适的答案。

我不确定这是否是您想要的。
但是您可以使用浮动图像按钮,使用窗口管理器。但这样按钮将是一个独立的覆盖,不再是4按钮视图的一部分。请参阅本文:

我建议您使用框架布局。这将使您的ImageView可堆叠

<FrameLayout>
    <ImageButton/> 
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
</FrameLayout>

您需要将它们全部设置为基于其父原点在屏幕上的静态位置。第5个ImageButton,您可以使用它像您那样设置onTouch侦听器

编辑:或者您可以这样做(如果您想保持线性布局):



只需将mainHut上的marginTop更改为要设置的偏移量

我用这些信息编辑了这篇文章。你提到过。对不起,应该说得更详细些。我觉得这不是我想要的方式。您向我展示的示例是使用一个服务,之前我尝试在服务中使用imagebutton,但没有成功。但谢谢你的想法。你使用的是线性布局,你只是在调整你的“主小屋”的边距。线性布局中的边距基本上会推动其他内容。一种方法可能是使用FrameLayout你必须使用android:layout_marginTop,你所说的挤压是什么意思?有了4个非拖放图像按钮,似乎可以将它们展平。如果我在你上次建议后更改android:layout_marginTopIts,则拖放图像按钮不会起任何作用。但是因为我使用的是framelayout,所以我的拖放imagebutton不再移动。有什么建议吗?谢谢你的帮助。你确定你使用的是FrameLayout.LayoutParams而不是LinearLayout.LayoutParams吗?我解决了这个问题,将拖放图像按钮放在整个FrameLayout内部自己的LinearLayout中,但与包含4个非拖放图像按钮的LinearLayout分开。问题解决了。现在我只需要在移动过程中保持图像以手指为中心。谢谢
<FrameLayout>
    <ImageButton/> 
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
    <ImageButton/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gllayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/mars"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/scores"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/polarCapButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/polarcap"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/polarCapButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="575dp"
            android:layout_marginTop="-70dp"
            android:background="@drawable/polarcap"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/spaceRockButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:background="@drawable/spacerock"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/spaceRockButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="520dp"
            android:layout_marginTop="-140dp"
            android:background="@drawable/spacerock"
            android:orientation="vertical" />

        <ImageButton
            android:id="@+id/meteor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="400dp"
            android:layout_marginTop="-100dp"
            android:background="@drawable/meteor"
            android:orientation="vertical"
            android:visibility="invisible" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/mainHut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="put offset here"
        android:background="@drawable/mainhut" />

</FrameLayout>