填充不适用于ImageButton

时间:2013-12-12 14:08:20

标签: 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" />

为什么padding没有做任何事情的想法?

完整的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>

4 个答案:

答案 0 :(得分:104)

您必须添加到您的ImageButton定义

android:scaleType="fitCenter"

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

答案 1 :(得分:0)

填充仅对import requests url ='http://192.168.17.179/data/vhg/1112' file_content = {'firewall.xml': open('firewall.xml','rb')} header = {'Content-Type': 'raw'} r= requests.put(url, files=file_content, headers=header) print(r.status_code) 属性有效,而不对android:src有效。

将第一个设置为按钮图像,将后者设置为android:background

答案 2 :(得分:0)

如果需要使用$PATH

设置较小的图像,则最好使用 ImageView ImageButton

答案 3 :(得分:0)

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

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