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


当前回答

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

<?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。

其他回答

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/按钮。

另一个简单的解决方案可以通过嵌入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" />

正如cephus提到的android:drawablePadding只会在按钮足够小的情况下在文本和可绘制对象之间强制填充。

当布局较大的按钮时,你可以使用android:drawablePadding和android:paddingLeft和android:paddingRight来强制文本和可绘制的向内朝向按钮的中心。通过分别调整左右填充,你可以对布局进行非常详细的调整。

下面是一个示例按钮,它使用填充物将文本和图标推得比默认情况下更近:

<Button android:text="@string/button_label" 
    android:id="@+id/buttonId"
    android:layout_width="160dip"
    android:layout_height="60dip"
    android:layout_gravity="center"
    android:textSize="13dip"
    android:drawableLeft="@drawable/button_icon"
    android:drawablePadding="2dip"
    android:paddingLeft="30dip"
    android:paddingRight="26dip"
    android:singleLine="true"
    android:gravity="center" />  

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

<?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。