因此,我已经设置了4个普通的imagebutton,然后是拖放的第5个imagebutton。我希望4个非拖放imagebutton都停留在特定位置,而不管拖放imageimage发生了什么。当我移动拖放图像按钮时,其他图像按钮也被移动并且也被挤压。
知道如何在不影响其他图像按钮的情况下移动此拖放图像按钮吗?
这是我的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
先谢谢您的帮助。
最佳答案
我建议您使用FrameLayout。这将使您的图像视图可堆叠。
<FrameLayout>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
</FrameLayout>
您需要将它们全部设置为基于其父原点在屏幕上具有静态位置。第五个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更改为您要设置的偏移量即可!