是否可以为我们添加的android: drawablleft图像设置边缘或填充?


当前回答

尝试使用负填充

如:

android:paddingLeft="-8dp"

其他回答

而不是按钮使用LinearLayout与ImageView和TextView里面。在子项目,如ImageView和TextView使用android:duplicateParentState="true"。

textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);

addressTitleView.setCompoundDrawablePadding();

另一个简单的解决方案可以通过嵌入layerlist实现

layered_drawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <inset
            android:insetRight="30dp"
            android:drawable="@drawable/ic_air_date">
        </inset>
    </item>
</layer-list>

XML中的按钮

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/layered_drawable"
    android:text="something" />

android:drawablePadding只会在文本和可绘制对象之间创建一个填充间隙,如果按钮足够小,可以将两者挤压在一起。如果你的按钮比组合宽度(用于drawablleft /drawableRight)或高度(用于drawableTop/drawableBottom)更宽,那么drawablePadding不会做任何事情。

我现在也在为这个问题挣扎。我的按钮很宽,图标挂在按钮的左边缘,文本在中间居中。我唯一的方法来解决这个问题,现在已经烘焙在可绘制的边缘,通过添加空白像素到画布的左边缘用photoshop。不理想,也不推荐。但这是我目前的权宜之计,短的重建TextView/按钮。

TextView有一个android:drawablePadding属性,这应该做的伎俩:

android:drawablePadding The padding between the drawables and the text. Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters). This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type. This corresponds to the global attribute resource symbol drawablePadding.