Removing Image Button Padding (Android)

2019-01-20 10:30发布

I have Buttons that look like the top image for the landscape orientation:

screenshot http://i46.tinypic.com/33zb9dj.jpg

I want it to look like the bottom image.

Here's my .xml code for these buttons.

<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>

5条回答
smile是对你的礼貌
2楼-- · 2019-01-20 10:45

An easier alternative to creating your own 9 patch image to remove the margin is to set the background to null.

For example, add the following to 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>

You should now find that all ImageButtons have zero margin, regardless of whether defined in XML or created programatically.

查看更多
欢心
3楼-- · 2019-01-20 10:54

//remove the android:weightSum use width as match_parent

//give android:layout_weight="1" for all imageButton

//use android:scaleType="fitXY" for all

<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>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-20 10:56

Use negative padding on the ImageButton in your xml to shrink the padding used.

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

Note the above is just an example... you'll need to mess with the numbers to adjust for your specific case to make things look correct.

查看更多
淡お忘
5楼-- · 2019-01-20 10:57

I ended up fixing this by switching my TableLayout to a LinearLayout. Here's my code unless anyone else comes across this problem:

<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>

查看更多
乱世女痞
6楼-- · 2019-01-20 11:09

You cannot do that with the default buttons, the buttons 9 patches have a padding in them (both, holo and the pre holo buttons). You can see that here.

If you want buttons without padding then you'll have to modify the 9 patches and create your own theme:

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

and your imageButtonStyle:

<style name="myImageButton" parent="Widget.ImageButton">
    <item name="android:background">@drawable/myCostomizedNinePatchSelectorWithoutPadding</item>
</style>
查看更多
登录 后发表回答