【问题标题】:How to center a drawable in a button?如何在按钮中居中绘制可绘制对象?
【发布时间】:2018-07-27 14:41:21
【问题描述】:

我想将图像(可绘制)放入按钮中。但是选项是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!

【问题讨论】:

  • 这是否意味着您不需要文本?
  • 我不想要文本,我想做一个底部导航,但这个小部件将图标放在顶部。所以我使用普通按钮来实现这个功能。但我不知道如何放置图像中心。对不起我的英语不好:(
  • 然后使用ImageButton
  • 但我无法使用 ImageButton 放置可绘制对象。背景拉伸图像...
  • 按钮必须是屏幕宽度的 1/3。

标签: java android xml button


【解决方案1】:

尝试像这样使用 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" />

【讨论】: