8

我的按钮看起来像横向的顶部图像:

截屏

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

这是我的这些按钮的 .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>
4

6 回答 6

8

您无法使用默认按钮执行此操作,按钮 9 个补丁中有一个填充(全息和全息前按钮)。你可以在这里看到。

如果您想要没有填充的按钮,那么您必须修改 9 个补丁并创建自己的主题:

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

和你的 imageButtonStyle:

<style name="myImageButton" parent="Widget.ImageButton">
    <item name="android:background">@drawable/myCostomizedNinePatchSelectorWithoutPadding</item>
</style>
于 2012-12-31T20:46:30.050 回答
1

创建您自己的 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 中定义还是以编程方式创建。

于 2013-07-12T10:13:01.427 回答
1

我最终通过将我的 TableLayout 切换到 LinearLayout 来解决这个问题。这是我的代码,除非其他人遇到这个问题:

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

于 2013-01-03T19:57:45.957 回答
0

//删除android:weightSum使用宽度为match_parent

//给android:layout_weight="1"所有的imageButton

//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>
于 2012-12-31T21:30:36.697 回答
0

在 xml 中使用负填充ImageButton来缩小使用的填充。

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

请注意,以上只是一个示例……您需要弄乱数字以针对您的特定情况进行调整,以使事情看起来正确。

于 2012-12-31T20:11:14.893 回答
0

使用scaleType属性,你可以看到相同大小的图标ImageButton

于 2021-12-09T13:01:25.610 回答