简体   繁体   English

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

[英]How to show a background image resized into ImageButton size?

I have a multi level LinearLayout to show 4x6 ImageButtons for an advent calendar.我有一个多级 LinearLayout 来显示出现日历的 4x6 ImageButtons。 But when I want to set the background of the ImageButton, it is too big.但是当我想设置ImageButton的背景时,它太大了。 How can I set the image background to fit the ImageButton size?如何设置图像背景以适合 ImageButton 大小? I want to show 24 pieces of present boxes in a nice grid.我想在一个漂亮的网格中显示 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" />

I think that what you want to do can be done changing/adding 2 things:我认为你想做的事情可以通过改变/添加两件事来完成:

  1. Its an ImageButton, so dont use the android:background property, but the android:src with the same drawable它是一个 ImageButton,所以不要使用android:background属性,而是使用具有相同 drawable 的android:src

  2. Then add the property android:adjustViewBounds="true" which basically adjusts the image to fit the view while maintaining aspect ratio然后添加属性android:adjustViewBounds="true"基本上调整图像以适应视图,同时保持纵横比

I think that should do the trick, else you can also try我认为这应该可以解决问题,否则您也可以尝试

  1. Use the android:scaleType property, I would try using fitXY but here are all the possible scale types: https://developer.android.com/reference/android/widget/ImageView.ScaleType使用android:scaleType属性,我会尝试使用fitXY但这里是所有可能的比例类型: https : //developer.android.com/reference/android/widget/ImageView.ScaleType

EDIT: Also, you might need to change your width and height properties, either specify a size in dp, use constraints, or wrap your ImageButtons in a GridLayout编辑:此外,您可能需要更改宽度和高度属性,以 dp 为单位指定大小,使用约束,或将 ImageButtons 包装在GridLayout 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM