我正在 Android 中玩 TicTacToe。当我点击一个 ImageButton 时,显示应用程序的图像会放大组件,而不考虑按钮的大小。
我使用了属性android:scaleType="fitCenter"
(几乎是fitXY
, centerInside
, fitCenter
, fitStart
) 尝试使图像适合组件内部,但没有成功。
这是我的 ImageButton 的 XML
代码的一部分:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<ImageButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:text="" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitCenter"
android:text="" />
<ImageButton
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitCenter"
android:text="" />
</LinearLayout>
我没有考虑我运行应用程序的 Android 设备(因为我在 Android 虚拟设备上运行游戏并且它发生了同样的问题)。
对于解决我的问题的任何帮助,我将不胜感激。谢谢。
最佳答案
如果线性布局的方向是水平的,那么在为它的 child 设置权重时,你应该设置它的宽度=“0dp”,垂直方向的高度=0dp。并使用 fitXY。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />
<ImageButton
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />
<ImageButton
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.246"
android:onClick="onDonGato"
android:scaleType="fitXY"
android:text="" />
</LinearLayout>
希望对您有所帮助。
关于java - 使图像适合 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26173954/