【问题标题】:ImageButton drawable background not centered and croppedImageButton 可绘制背景未居中和裁剪
【发布时间】:2016-05-08 14:23:10
【问题描述】:

我有这个 ImageButton

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/crimson_round_button"
        android:src="@drawable/play_white"/>

crimson_round_button.xml 可绘制对象在哪里

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape android:shape="oval">
                <solid android:color="@color/hibiscus"/>
                <stroke
                    android:width="4dp"
                    android:color="@color/crimson"/>
            </shape>
        </item>

        <item>
            <shape android:shape="oval">
                <solid android:color="@color/crimson"/>
                <stroke
                    android:width="4dp"
                    android:color="@color/crimson"/>
            </shape>
        </item>
    </selector>

而 play_white.png 可绘制对象仅代表按钮中的白色播放图标。

我的最终结果是这个

您可以看到内部图标未居中,并且您可以注意到右下边框被裁剪了一点。

原因是应用的背景不适合 ImageButton,正如您在这张图片中看到的那样,我在 Android Studio 预览中选择了 ImageButton

知道为什么会这样吗?


编辑:

好的,让我们试着忘记白色箭头。 这是我的图像按钮(及其容器)的新代码

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:background="@color/white">

        <ImageButton
            android:id="@+id/save_record_dialog_play_imageButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:background="@drawable/crimson_round_button"/>
    </FrameLayout>

你可以看到设置为 ImageButton 背景的圆圈不再是一个圆圈,它在左侧和底部被裁剪

为 ImageButton 设置 4dp 高度,裁剪后的边框更加明显

【问题讨论】:

标签: android background imagebutton


【解决方案1】:

我认为问题在于三角形的播放按钮看起来并不居中。

我用我自己的三角形按钮尝试了你的布局:

看起来它离右边太远了,但是如果我们像这样描绘三角形按钮的边界(对不起我的绘画技巧):

图标本身似乎是居中的。

一种解决方案是调整左边的内边距,使其看起来更好:

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/crimson_round_button"
    android:src="@drawable/play_white"
    android:paddingLeft="3dp"/>

这给出了这个:

关于您问题的第二部分,您是否观察到设计选项卡上的裁剪?在模拟器中?在设备上?对我来说,我在布局设计选项卡上看到了这个问题:

但不在设备上(这是来自屏幕截图):

【讨论】:

  • 天啊! @Michiyo你说得对,这只是Android Studio和模拟器​​中预览的问题(如果不是真的缩放的话)!
猜你喜欢
  • 2021-11-30
  • 2019-11-14
  • 2021-07-05
  • 2015-10-22
  • 2023-03-05
  • 1970-01-01
  • 2013-12-03
  • 2011-10-10
  • 1970-01-01
相关资源
最近更新 更多