【问题标题】:How can I stretch the image in imagebutton to the whole area of the imagebutton?如何将 imagebutton 中的图像拉伸到 imagebutton 的整个区域?
【发布时间】:2013-02-14 13:15:18
【问题描述】:

这是一张可以帮助你理解我的问题的图片:

我想将ImageButton 中显示的图像拉伸到ImageButton 的整个区域。 可以看到,编号为1的图片只占ImageButton的95%左右,还可以看到ImageButton的边界。

我还是要保持图片按钮样式

这是我使用 ImageView 的方式:

<ImageButton
    android:id="@+id/secondActivityBbuttonDownRight"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:scaleType="fitStart"
    android:src="@drawable/111" />

我想我发现了我的问题所在。 我需要一个自定义组件,它可以只包含一张图片并具有点击效果。

【问题讨论】:

  • 使用这个 android:background="@drawable/111"
  • 将 android:background="@android:color/transparent" 添加到 ImageButton
  • 我编辑了您的帖子,请参阅 meta.stackexchange.com/questions/85516 了解如何在您的帖子中添加图片。
  • 我没有足够的声望点来这样做
  • 我只是在 ImageButton xml 中添加了这个 - android:background="@null"

标签: android image imagebutton stretch


【解决方案1】:

ImageButton 是 ImageView。
如果您希望图像填充按钮
比设置android:scaleType="fitXY",所以你在android:src中设置的图像将被拉伸以填充整个图像按钮。

ImageButton 在您的图片和内边距下方有一个背景。
如果你不想要背景
设置android:background="@android:color/transparent".
如果你不想填充
设置android:padding="0" 或设置不带填充的背景。

【讨论】:

  • 好的答案谢谢,但你需要填充单位:android:padding="0dip"
  • 我认为填充不再是 ImageButton 的一部分
【解决方案2】:

你也可以使用

android:scaleType="centerCrop"
android:padding="0dp"

【讨论】:

    【解决方案3】:

    android:scaleType="fitXY" 测试并正常工作。

    【讨论】:

      【解决方案4】:

      使用下面的代码:-

      <ImageButton
              android:id="@+id/secondActivityBbuttonDownRight"
              android:layout_width="0dip"
              android:layout_height="match_parent"
              android:layout_weight="0.5"
              android:scaleType="fitStart"
              android:background="@drawable/111" />
      

      【讨论】:

      • 这并不能解决我的问题。我想使用图片按钮的样式并拉伸图片
      • 与背景一起使用的比例类型(不适用于背景...)
      【解决方案5】:

      您应该考虑创建一个 9-patch 图像,以使图像正确适合 ImageButton,以防您需要拉伸它,但又不希望图像变形。

      要拥有一个带有完全覆盖它的图像的按钮,您可以使用以下内容:

      <ImageButton android:id="@+id/iconBtn"
        android:layout_width="64dip"
        android:layout_height="64dip"
        android:src="@drawable/icon_button"
        android:scaleType="fitCenter"
        android:background="#00000000"/>
      

      有关使用自定义外观创建按钮以及使用选择器管理其状态的完整教程,您可以查看我前段时间在此处编写的教程(Creating a custom Android button with a resizable skin)

      【讨论】:

        【解决方案6】:

        如果您的图像“111”本身没有填充,则只需使用 android:background"@drawable/111"

        【讨论】:

          【解决方案7】:

          只需在ImageButton上设置这两个属性:

          android:background="?attr/selectableItemBackgroundBorderless"
          android:scaleType="centerCrop"
          

          有了这些,您将获得提供的 src 进行拉伸,而且如果您在 ImageButton 上放置一些特定的 widthheight,您将不会在它周围得到一个矩形。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多