【问题标题】:How to show a background image resized into ImageButton size?如何显示调整为 ImageButton 大小的背景图像?
【发布时间】:2021-02-24 08:02:11
【问题描述】:

我有一个多级 LinearLayout 来显示出现日历的 4x6 ImageButtons。但是当我想设置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" />

【问题讨论】:

  • 您是否尝试过将高度和宽度添加为像素而不是 wrap_content?

标签: android android-studio imagebutton


【解决方案1】:

我认为你想做的事情可以改变/添加两件事:

  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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2017-01-04
    相关资源
    最近更新 更多