删除图像按钮填充(Android)

删除图像按钮填充(Android),android,image,button,padding,Android,Image,Button,Padding,我有一些按钮,看起来像横向方向的顶部图像: 我希望它看起来像底部的图像 下面是这些按钮的.xml代码 <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:weightSum="1.0" >

我有一些按钮,看起来像横向方向的顶部图像:

我希望它看起来像底部的图像

下面是这些按钮的.xml代码

<TableRow
    android:id="@+id/tableRow7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:weightSum="1.0" >

    <ImageButton
        android:id="@+id/appHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="@null"
        android:contentDescription="Home"
        android:cropToPadding="true"
        android:scaleType="fitEnd"
        android:src="@drawable/home_bar" />

    <ImageButton
        android:id="@+id/menuHome"
        android:layout_weight="0"
        android:background="@null"
        android:contentDescription="Menu"
        android:cropToPadding="true"
        android:scaleType="center"
        android:src="@drawable/menu_bar" />

    <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="@null"
        android:contentDescription="Back"
        android:cropToPadding="true"
        android:scaleType="fitStart"
        android:src="@drawable/back_bar" />

</TableRow>

在xml中的
ImageButton
上使用负片填充以缩小所使用的填充

android:paddingLeft="-5dip"
android:paddingRight="-5dip"

请注意,以上只是一个例子。。。您需要根据具体情况调整数字,以使事情看起来正确。

默认按钮无法做到这一点,按钮9补丁中有一个填充(全息和全息前按钮)。 你可以看到

如果您希望按钮没有填充,则必须修改9个补丁并创建自己的主题:

<style name="myTheme" parent="....">
    <item name="imageButtonStyle">@style/myImageButton</item>
</style>

@样式/myImageButton
以及您的ImageButton样式:

<style name="myImageButton" parent="Widget.ImageButton">
    <item name="android:background">@drawable/myCostomizedNinePatchSelectorWithoutPadding</item>
</style>

@无需添加的可抽出式/霉菌化NinePatchSelector

//删除
android:weightSum
使用宽度作为
匹配\u父项

//为所有图像按钮提供
android:layout\u weight=“1”

//使用android:scaleType=“fitXY”进行所有操作

<TableRow
    android:id="@+id/tableRow7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >

    <ImageButton
        android:id="@+id/appHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@null"
        android:contentDescription="Home"
        android:cropToPadding="true"
        android:scaleType="fitXY"
        android:src="@drawable/home_bar" />

    <ImageButton
        android:id="@+id/menuHome"
        android:layout_weight="0"
        android:background="@null"
        android:contentDescription="Menu"
        android:layout_weight="1"
        android:cropToPadding="true"
        android:scaleType="fitXY"
        android:src="@drawable/menu_bar" />

    <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_weight="1"
        android:background="@null"
        android:contentDescription="Back"
        android:cropToPadding="true"
       android:scaleType="fitXY"
        android:src="@drawable/back_bar" />

</TableRow>

我最终通过将表格布局切换为线性布局解决了这个问题。以下是我的代码,除非其他人遇到此问题:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center" >

    <ImageButton
        android:id="@+id/appHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="Home"
        android:scaleType="fitEnd"
        android:src="@drawable/home_bar_grey" />

    <ImageButton
        android:id="@+id/menuHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="Menu"
        android:onClick="menuhome"
        android:scaleType="fitCenter"
        android:src="@drawable/menu_bar" />

    <ImageButton
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="Back"
        android:scaleType="fitStart"
        android:src="@drawable/exit_bar" />
</LinearLayout>


创建自己的9面片图像以删除边距的一个更简单的替代方法是将背景设置为空

例如,将以下内容添加到res/styles.xml:

<style name="MarginlessImageButton" parent="android:Widget.ImageButton">
    <item name="android:background">@null</item>
</style>

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:imageButtonStyle">@style/MarginlessImageButton</item>
</style>

@空的
@样式/边框图像按钮

您现在应该会发现,所有ImageButton都没有边距,不管是用XML定义的还是用编程方式创建的。

无论数字有多高,我仍然得到相同的奇怪填充。它可能是边距而不是填充。。。同样的技巧也适用,使用负数。我刚刚尝试了相同结果的边距。。。我两个都试过了。仍然存在所有不可见的空间。这是可行的,但它延伸了图像,这是我不想要的。然后删除android:scaleType=“fitXY”。当我这样做时,我获得了与以前相同的结果(使用填充空间)将background设置为null的问题是,单击ImageButton时会丢失灰色色调。请检查此选项