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


当前回答

如果可绘制资源的大小是固定的,你可以这样做:

<Button
    android:background="@drawable/rounded_button_green"
    style="?android:attr/selectableItemBackground"
    android:layout_height="wrap_content"
    app:layout_widthPercent="70%"
    android:drawableRight="@drawable/ic_clear_black_24dp"
    android:paddingRight="10dp"
    android:paddingLeft="34dp"
    tools:text="example" />

这里的关键是:

    android:drawableRight="@drawable/ic_clear_black_24dp"
    android:paddingRight="10dp"
    android:paddingLeft="34dp"

也就是说,可绘制资源的大小加上paddingRight等于paddingLeft。

您可以在本例中看到结果

其他回答

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

是的。使用drawablePadding,如下所示

<TextView
        android:id="@+id/tvHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Settings and Contents"
        android:drawableLeft="@drawable/icon_success"
        android:drawablePadding="10dp" />

android:drawablePadding是给可绘制图标填充最简单的方法,但你不能给特定的一侧填充,如paddingRight或paddingLeft的可绘制图标。要做到这一点,你必须深入研究。 如果你将paddingLeft或paddingRight应用到Edittext,那么它会将填充到整个Edittext以及可绘制的图标。

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

<?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.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);

addressTitleView.setCompoundDrawablePadding();