如何在Android中更改ImageButton的位置
单击ImageButton时,如何更改其位置 这里有6个图像按钮:如何在Android中更改ImageButton的位置,android,position,Android,Position,单击ImageButton时,如何更改其位置 这里有6个图像按钮:ibWolf1、ibWolf2、ibWolf3、ibSheep1、ibSheep2、ibSheep31个图像视图ivWood和1个按钮btnGoAndBack。当我单击ibSheep3时,ibSheep3将改变如下位置: 当我继续点击ibWolf3时,它将改变如下位置: 这意味着图像将显示在木材图像上方。那么如何做到这一点呢 这是我的game_play_layout.xml: <?xml version="1.0" e
ibWolf1、ibWolf2、ibWolf3、ibSheep1、ibSheep2、ibSheep3代码>1个图像视图ivWood
和1个按钮btnGoAndBack
。当我单击ibSheep3时,ibSheep3
将改变如下位置:
当我继续点击ibWolf3时,它将改变如下位置:
这意味着图像将显示在木材图像上方。那么如何做到这一点呢
这是我的game_play_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Go"
android:textSize="30dp"
android:background="#3300FF00"
android:id="@+id/btnGoAndBack"
android:layout_below="@+id/ivWood"
android:layout_toLeftOf="@+id/ibSheep2"
android:layout_toStartOf="@+id/ibSheep2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/wolf"
android:scaleType="centerCrop"
android:id="@+id/ibWolf1"
android:layout_above="@+id/ibSheep2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/wolf"
android:scaleType="centerCrop"
android:id="@+id/ibWolf2"
android:layout_below="@+id/ibWolf1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/wolf"
android:scaleType="centerCrop"
android:id="@+id/ibWolf3"
android:layout_below="@+id/ibWolf2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/sheep"
android:scaleType="centerCrop"
android:id="@+id/ibSheep1"
android:layout_above="@+id/ibWolf2"
android:layout_toLeftOf="@+id/ibWolf1"
android:layout_toStartOf="@+id/ibWolf1" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/sheep"
android:scaleType="centerCrop"
android:id="@+id/ibSheep2"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ibWolf2"
android:layout_toStartOf="@+id/ibWolf2" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/sheep"
android:scaleType="centerCrop"
android:id="@+id/ibSheep3"
android:layout_below="@+id/ibSheep2"
android:layout_toLeftOf="@+id/ibWolf3"
android:layout_toStartOf="@+id/ibWolf3" />
<ImageView
android:layout_width="200dp"
android:layout_height="30dp"
android:src="@drawable/wood"
android:scaleType="centerCrop"
android:id="@+id/ivWood"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/ibSheep2"
android:layout_toStartOf="@+id/ibSheep2"/>
</RelativeLayout>
一个简单的解决方案是,在go按钮上方放置两张绵羊和狼的图像,首先使可见性不可见或消失。现在,当用户单击绵羊或狼图像时,使imagebutton的可见性消失,并使go按钮上方的图像可见
否则,您必须计算go按钮的位置,并以编程方式创建imagebutton并将其放置在那里
希望它能清除您的视图。因此我必须在wood ImageView上方再添加两个ImageButton。但是这种方式很难处理这个项目。是的,但我不认为这会很难处理。你为什么这么认为?你所要做的就是检查按钮的侦听器。我想创建一个游戏,让所有6种动物都能过河,每次当狼的数量>羊的数量-->游戏结束时。我会先试试你的方法,但我需要一些更好的方法。明白了。让我们看看是否有人有更好的办法。计算所有的位置比我建议的要难。祝你好运。如果我们使用翻译,我想它实际上不会在不同的设备上运行。