【问题标题】:Padding not working on ImageButton填充在 ImageButton 上不起作用
【发布时间】:2013-12-30 23:54:52
【问题描述】:

在我正在开发的应用程序中,我有几个 ImageButtons。每个 ImageButton 都有一个可绘制形式的背景和内容。现在,drawable 在 ImageButton 的范围内处于最大尺寸,但我希望它按比例缩小,所以我需要添加一些填充。问题是,当我尝试这样做时,它没有任何效果。对于每个 ImageButton,我的 XML 如下:

<ImageButton
    android:id="@+id/button_zero"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:padding="10dip"
    android:src="@drawable/button_zero"
    android:background="@drawable/button_background" />

任何想法为什么填充不做任何事情?

完整的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#222222"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <!-- Row One -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <ImageButton
                android:id="@+id/button_zero"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_zero"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_one"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_one"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_two"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_two"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_three"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_three"
                android:background="@drawable/button_front" />
            <ImageButton
                android:id="@+id/button_four"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:padding="10dip"
                android:src="@drawable/button_four"
                android:background="@drawable/button_front" />
        </LinearLayout>

        ... same for other rows

    </LinearLayout>

【问题讨论】:

  • 你能添加你所有的 XML 代码吗?
  • @user2235615 我更新了问题:)

标签: android padding imagebutton


【解决方案1】:

您必须添加到您的 ImageButton 定义中

android:scaleType="fitCenter"

或者像 fitXY 这样的其他 scaleType,因为默认情况下图像会尽可能地缩放并忽略填充

【讨论】:

  • 更好的是 ScaleX=1,4 和 ScaleX=1,4
【解决方案2】:

填充仅对android:src 属性有效,对android:background 无效。

将第一个设置为您的按钮图像,将后者设置为android:background="@android:color/transparent"

【讨论】:

  • 我会敦促人们使用?attr/selectableItemBackground 而不是透明的,以便基于主题配置的触摸反馈是机智的。
【解决方案3】:

如果您需要使用 padding 将图像设置得更小,则最好使用 ImageView 而不是 ImageButton

【讨论】:

    【解决方案4】:

    关于scaleType默认的问题,在fitXY的ImageButton中,你只需要改变fitCenter即可。

    <ImageButton
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:paddingVertical="4dp"
                android:scaleType="fitCenter" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 2018-01-19
      • 2015-07-17
      • 1970-01-01
      • 2012-08-02
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多