微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何显示调整为ImageButton大小的背景图像?

如何解决如何显示调整为ImageButton大小的背景图像?

我有一个多层LinearLayout来显示出现日历的4x6图像按钮。但是,当我要设置ImageButton的背景时,它太大了。如何设置图像背景以适合ImageButton的大小?我想在一个漂亮的网格中显示24个礼物盒。

<ImageButton
            android:id="@+id/buttonOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="25"
            android:background="@drawable/present"
            android:fontFamily="@font/christmas"
            android:onClick="playOne"
            android:text="1"
            android:textColor="@android:color/white"
            android:textSize="@dimen/button_text_size" />

解决方法

我认为您想要做的事情可以通过更改/添加两件事来完成:

  1. 它是一个ImageButton,所以不要使用android:background属性,而要使用android:src并具有相同的可绘制对象

  2. 然后添加属性android:adjustViewBounds="true",该属性基本上可以在保持宽高比的同时调整图像以适合视图

我认为应该可以解决问题,否则您也可以尝试

  1. 使用android:scaleType属性,我会尝试使用fitXY,但这是所有可能的比例类型:https://developer.android.com/reference/android/widget/ImageView.ScaleType

编辑:另外,您可能需要更改宽度和高度属性,或者以dp指定大小,使用约束或将ImageButtons包装在GridLayout

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。