【问题标题】:how do I make an image fit in my button如何使图像适合我的按钮
【发布时间】:2015-11-03 04:40:03
【问题描述】:

每次我将图像添加到我的按钮(想将其添加到文本下方)时,它只会显示图像的左上角,并且即使我使用 android:drawableBottom 。

这是我的 XML 代码:

<Button
    android:layout_width="162dp"
    android:layout_height="200dp"
    android:text="@string/weather_station"
    android:textStyle="bold"
    android:textSize="18dp"
   android:background="@drawable/weather"
    android:paddingBottom="120dp"
    android:paddingLeft="5dp"
    android:typeface="monospace"
    android:id="@+id/weather_station" />
    android:drawableBottom="@drawable/weatherman"

    />

【问题讨论】:

  • 为什么不使用一个ViewGroup(如垂直LinearLayout)来代替Button,并把ImageView和TextView作为children,然后给ViewGroup设置一个onClickListener?
  • 用textview代替button,然后可以设置drawable bottom
  • 我使用了按钮,它对其中一个有效,然后我尝试了另一个,但我仍然有同样的错误。实际上我在页面上制作了 4 个按钮

标签: android android-layout android-activity android-button


【解决方案1】:

使用ImageButton 而不是Button 并尝试缩放图像`

<ImageButton
    android:id="@+id/my_image_button"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:src="@drawable/my_img" />

【讨论】:

    【解决方案2】:
    <Button
        android:layout_width="162dp"
        android:layout_height="200dp"
        android:text="bUTTON"
        android:textStyle="bold"
        android:textSize="18dp"
        android:background="@drawable/shape1"
        android:paddingBottom="120dp"
        android:paddingLeft="5dp"
        android:drawableBottom="@drawable/cast_ic_notification_2"
        android:typeface="monospace"
        android:id="@+id/weather_station" />
    

    你的可绘制线怎么可能超出范围?试试上面的工作代码

    【讨论】:

    • 其实是在范围内,只是因为我粘贴时没有画线,自己写在stackoveflow上。
    【解决方案3】:

    尝试在 xml 中设置以下比例类型以在按钮中填充图像。

    android:scaleType="scaleXY"
    

    所以把这个属性放在你的 xml 代码中。

     <Button
     android:layout_width="162dp"
     android:layout_height="200dp"
     android:text="@string/weather_station"
     android:textStyle="bold"
     android:textSize="18dp"
     android:background="@drawable/weather"
     android:paddingBottom="120dp"
     android:scaleType="scaleXY"
     android:paddingLeft="5dp"
     android:typeface="monospace"
     android:id="@+id/weather_station"
     android:drawableBottom="@drawable/weatherman"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 2016-10-15
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多