java - 使图像适合 ImageButton

标签 java android android-imagebutton

我正在 Android 中玩 TicTacToe。当我点击一个 ImageButton 时,显示应用程序的图像会放大组件,而不考虑按钮的大小。

TicTacToe

我使用了属性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/

相关文章:

java - 可能返回一个字符串数组

java - 方法无法解析为 Eclipse 中的变量

java - Facebook GRAPH API - 登录绕过。代币安全

android - 由于找不到符号 picasso.fit(),无法构建 apk;

java - 一个 ImageButton 可以工作,其他的则崩溃应用程序

java - Android,如何实现 ImageButton 的圆角,然后以编程方式更改颜色?

java - 如何发出仅更新提供的字段的 POST 请求?

java - guava的ImmutableList并不是真的不可变

java - 模拟器错误: user data image is used by another emulator

android - 如何在不旋转布局的情况下改变方向时旋转按钮?