【发布时间】: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