填充不适用于ImageButton

Zer*_*ero 46 android padding imagebutton

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

<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" />
Run Code Online (Sandbox Code Playgroud)

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

完整的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>
Run Code Online (Sandbox Code Playgroud)

Goo*_*iec 110

您必须添加到您的ImageButton定义

android:scaleType="fitCenter"
Run Code Online (Sandbox Code Playgroud)

或其他scaleType,如fitXY,因为默认情况下图像尝试尽可能地缩放并忽略填充


PJ_*_*gan 6

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

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

  • 我强烈建议人们使用“?attr/selectableItemBackground”而不是透明的,以便基于主题配置的触摸反馈完好无损。 (3认同)