【问题标题】:ImageButtons being squished when my drag and drop ImageButton is moved当我的拖放 ImageButton 被移动时,ImageButtons 被压扁
【发布时间】:2014-06-10 19:08:28
【问题描述】:

所以我设置了 4 个普通的图像按钮,然后是第 5 个图像按钮,它是一个拖放操作。无论拖放的图像按钮发生什么,我都希望 4 个非拖放图像按钮留在特定位置。当我移动我的拖放图像按钮时,其他图像按钮也会被移动并被压扁。

知道如何在不以任何方式影响其他图像按钮的情况下移动此拖放图像按钮吗?

这是我的 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>

这是我拖放图像按钮的代码:

    mainHut = (ImageButton) findViewById(R.id.mainHut);
    mainHut.setOnTouchListener(new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                mainHutSelected = true;
            }//end if

            if (event.getAction() == MotionEvent.ACTION_UP)
            {
                if (mainHutSelected == true)
                {
                    mainHutSelected = false;
                }//end if
            }//end if
            else if (event.getAction() == MotionEvent.ACTION_MOVE)
            {
                if (mainHutSelected == true)
                {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
                    params.setMargins((int)(event.getRawX() - event.getRawY() / 2), (int) (event.getRawY() - v.getHeight() * 1.5), (int) (event.getRawX() - v.getWidth() / 2), (int) (event.getRawY() - v.getHeight() * 1.5));
                    v.setLayoutParams(params);
                }//end if
            }//end else

            return false;
        }//end onTouch function

    });//end setOnClickListener

提前感谢您的帮助。

【问题讨论】:

  • 您正在使用 LinearLayout 并且您只是在调整“mainhut”的边距。线性布局中的边距基本上会推动其他内容。一种方法可能是使用框架布局

标签: java android mobile drag-and-drop imagebutton


【解决方案1】:

我建议你使用 FrameLayout。这将使您的图像视图可堆叠。

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

您需要将它们全部设置为根据其父源在屏幕上的静态位置。第 5 个 ImageButton,你可以像你一样使用它来设置 onTouch 监听器。

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

<?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>

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

【讨论】:

  • 你必须使用 android:layout_marginTop 而你说的挤压是什么意思?
  • 好吧,有了 4 个非拖放图像按钮,它们似乎有点扁平化了。如果我更改 android:layout_marginTop,则拖放图像按钮不起作用
  • 在你最后的建议之后会更好。但是由于我使用的是框架布局,所以我的拖放图像按钮不再移动。有什么建议么?顺便感谢您的帮助。
  • 您确定您使用的是 FrameLayout.LayoutParams 而不是 LinearLayout.LayoutParams?
  • 我解决了我的问题,方法是将拖放图像按钮放在整体框架布局内部的自己的线性布局中,但与包含 4 个非拖放图像按钮的线性布局分开。问题解决了。现在我只需要修复在移动过程中保持图像以我的手指为中心。谢谢!
【解决方案2】:

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

【讨论】:

  • 我用信息编辑了帖子。你提到的。对不起,应该更详细。
【解决方案3】:

我不确定这是否是您想要的。 但是您可以使用浮动图像按钮,使用窗口管理器。但这样按钮将是一个独立的叠加层,不再是 4 按钮视图的一部分。看到这个帖子:What APIs in Android is Facebook using to create Chat Heads?

【讨论】:

  • 我觉得这不会像我想要的那样工作。您向我展示的示例是使用服务,我之前尝试在服务中使用图像按钮,但没有成功。不过谢谢你的想法。
猜你喜欢
  • 2021-06-24
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
相关资源
最近更新 更多