Android Button.setCompoundDrawablesWithIntrinsicBounds() 的左/右填充?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10875750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:18:47  来源:igfitidea点击:

Left/right padding for Button.setCompoundDrawablesWithIntrinsicBounds()?

android

提问by user291701

I'm trying to set a left icon on a button with:

我正在尝试在按钮上设置一个左图标:

setCompoundDrawablesWithIntrinsicBounds(R.drawable.foo, 0, 0, 0);

but the icon is placed flush up against the left edge of my button, and the text string. Is there a way to specify some left/right padding on the supplied icon so that it isn't right up against the edges?

但该图标与我的按钮和文本字符串的左边缘齐平放置。有没有办法在提供的图标上指定一些左/右填充,使其不紧靠边缘?

Thanks

谢谢

回答by antew

I believe what you're looking for is android:drawablePadding

我相信你正在寻找的是 android:drawablePadding

Here's an example of using drawablePaddingalong with paddingLeftand paddingRightto position an image in a button

这是一个使用drawablePaddingwithpaddingLeftpaddingRight在按钮中定位图像的示例

<Button
    android:id="@+id/button"
    android:layout_width="200dp"
    android:layout_height="80dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="2dip"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:text="Test" />

Image example

图片示例

回答by meda

For anyone looking for the programtic way, here is an example:

对于任何寻找程序化方式的人,这里有一个例子:

menuButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.foo, 0, 0, 0);
menuButton.setCompoundDrawablePadding(10);