首页 > 解决方案 > 如何在按钮中居中绘制可绘制对象?

问题描述

我想将图像(可绘制)放入按钮中。但是选项是drawableLeft/Bottom/Top/Rigth。我想把它放在中心。背景不起作用,因为它拉伸了我的图像。该图像是可绘制到可绘制文件夹中的。如果可能的话,xml中的代码。谢谢!

可绘制对象:

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/im_perfil"
        android:bottom="20dp"
        android:left="20dp"
        android:right="20dp"
        android:top="20dp"/>
</layer-list>

“im_perfil”是一个 ImageAsset,

我能!我用src!

标签: javaandroidxmlbutton

解决方案


尝试像这样使用 src 将 drawable 放入图像按钮

<ImageButton
 android:id="@+id/btn_id"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:background="@null"
 android:src="@drawable/im_perfil" />

推荐阅读