简体   繁体   English

ImageButton可绘制的背景未居中并裁剪

[英]ImageButton drawable background not centered and cropped

I have this ImageButton 我有这个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"/>

where crimson_round_button.xml drawable is 其中crimson_round_button.xml是drawable的

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

and the play_white.png drawable represents only the white play icon in the button. 而play_white.png drawable仅代表按钮中的白色播放图标。

My final result is this one 我的最终结果是这一个

在此输入图像描述

A you can see the inner icon is not centered and you can notice that the right and bottom borders are a little bit cropped. 你可以看到内部图标没有居中,你可以注意到右边框和下边框有点裁剪。

The reason is that the applied background is not fitting the ImageButton, as you can see in this image where I selected the ImageButton in Android Studio preview 原因是应用的背景不适合ImageButton,正如您在此图像中看到的,我在Android Studio预览中选择了ImageButton

在此输入图像描述

Any idea why this is happening? 知道为什么会这样吗?


EDIT: 编辑:

Ok, let's try to forget about the white arrow. 好吧,让我们试着忘记白色箭头。 This is my new code for the Image button (and its container) 这是我的Image按钮(及其容器)的新代码

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

as you can see the circle set as ImageButton background is no more a circle, it's cropped on the left side and at the bottom 你可以看到设置为ImageButton背景的圆圈不再是一个圆圈,它在左侧和底部被裁剪

在此输入图像描述 在此输入图像描述

and setting a 4dp elevation to the ImageButton the cropped border is much more visible 并将4dp高程设置为ImageButton,裁剪边框更加明显

在此输入图像描述

I think the problem is that the triangle-shaped play button just doesn't look centered. 我认为问题在于三角形的播放按钮看起来并不居中。

I tried your layout with my own triangle-shaped button: 我用自己的三角形按钮尝试了你的布局:

在此输入图像描述

It looks like it's too far to the right, but if we picture the bounds of the triangle button (sorry about my paint skills) like so: 看起来它太偏右了,但是如果我们想象三角形按钮的界限(抱歉我的绘画技巧)就像这样:

在此输入图像描述

the icon itself does seem to be centered. 图标本身似乎确实居中。

One solution is to adjust the left padding so it looks better: 一种解决方案是调整左边的填充,使其看起来更好:

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

Which gives this: 这给了这个:

在此输入图像描述

With regard to the second part of your question, have you observed the cropping on the design tab? 关于问题的第二部分,您是否观察过设计选项卡上的裁剪? In an emulator? 在模拟器中? On a device? 在设备上? For me, I see this issue on the layout design tab: 对我来说,我在布局设计选项卡上看到了这个问题:

布局设计选项卡

but not on a device (this is from a screenshot): 但不在设备上(这是截图): 设备截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM