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


当前回答

我也会把我的答案扔到擂台上。如果您希望以编程方式完成此操作,可以执行以下操作。

final Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.somedrawable);
final boolean isLTR = ViewCompat.LAYOUT_DIRECTION_LTR == ViewCompat.getLayoutDirection(this);
final int iconInsetPadding = getResources().getDimensionPixelSize(R.dimen.icon_padding);

final Drawable insetDrawable = new InsetDrawable(drawable, isLTR ? 0 : iconInsetPadding, 0, isLTR ? iconInsetPadding : 0, 0);

这将添加填充到可绘制的结束,结束将意味着左/右取决于电话是在LTR或RTL。

其他回答

你应该考虑使用图层列表

创建一个这样的可绘制文件,将其命名为ic_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
    </shape>
</item>
<item android:right="10dp">
    <bitmap android:gravity="center_vertical|left"
        android:src="@drawable/ic_calendar_16dp"
        android:tint="@color/red"
        />
</item>
</layer-list>

在布局文件下,

<TextView
         android:id="@+id/tvDate"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:drawableLeft="@drawable/ic_calendar"
         android:textColor="@color/colorGrey"
         android:textSize="14sp" 
    />

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.

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

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

为你的编辑文本定义一个形状,并给它一个填充 例如

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <padding
        android:left="5dp"
        android:right="5dp"
    />
    <solid android:color="#F6F6F6" />
    <stroke
        android:width="1px"
        android:color="#C3C3C3" />

    <corners
        android:bottomLeftRadius="1dp"
        android:bottomRightRadius="1dp"
        android:topLeftRadius="1dp"
        android:topRightRadius="1dp" />
</shape>

在此形状中定义的填充将帮助为可绘制的左侧或右侧提供填充 ---------------------- 应用这个EditView形状

 <EditText
            android:id="@+id/example"
            android:layout_width="fill_parent"
            android:layout_height="36dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/shape2"
            android:drawableLeft="@drawable/icon1"
            android:drawablePadding="@dimen/txtDrwblPadding"
            android:ems="10"
         />

使用定义的形状作为背景将给你的EditText一些风格加上边框drawablleft。

尝试使用负填充

如:

android:paddingLeft="-8dp"