【问题标题】:How to size an Image of a button? [duplicate]如何调整按钮图像的大小? [复制]
【发布时间】:2017-12-06 01:45:39
【问题描述】:

我有以下布局

            <Button
                android:layout_width="fill_parent"
                android:layout_height="75dp"
                android:drawableLeft="@drawable/binocular"
                android:gravity="left|center_vertical"
                android:text="search" />

它看起来像:

图片太大。我只是想将图像与corredct 大小对齐到左侧,因此它适合按钮的height

我知道我可以通过代码调整图像的边界。但我想知道,如果有办法处理layout specification

有一个解决方案:how to change a drawableLeft icon size on a button?(已接受答案)

我猜这是正确的,但我不明白,将layer-list 放在哪里以及如何在button 中引用它。

有人可以帮忙吗?

【问题讨论】:

  • 尝试使用图片按钮。
  • res/drawable文件夹中新建一个drawable文件

标签: java android layout


【解决方案1】:

您必须创建新的可绘制 xml 文件并在其中放置图层列表代码。例如 button_background.xml。

然后在按钮上设置这个drawable

android:drawableLeft="@drawable/button_background"

【讨论】:

  • 哇。而已。非常感谢。有人反对你的,我赞成你的正确答案。必须等待10分钟才能接受。谢谢
  • @mcflysoft 如果答案正确,您可以使用复选标记设置为已更正,以关闭问题。快乐编码:)
【解决方案2】:

ImageButton 上设置scaleType 属性

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

【讨论】:

    【解决方案3】:

    使用带有这些属性的 ImageButton

    <ImageButton
            android:id="@+id/button_topleft"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="0dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="20dp"
            android:adjustViewBounds="true"
            android:scaleType="centerInside"        
     />
    

    scaleType 失败后,填充在调整图像大小方面起着非常重要的作用。

    希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-07-04
      • 2013-05-12
      • 2016-09-17
      • 2022-12-09
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多