可抽出的正确位置不正确

时间:2019-04-08 16:07:49

标签: android android-drawable android-vectordrawable

我以编程方式添加了左右可绘制对象,但右上未添加可绘制对象右图标。

enter image description here

我做了什么:

ReusableClass.setVectorDrawable(LoginActivity.this, R.drawable.ic_language_black_24dp, R.drawable.ic_arrow_drop_down_black_24dp, inputLanguage, R.color.colorAccent);

public static void setVectorDrawable(Context context, int leftIcon, int rightIcon, EditText editText, int color) {
    editText.setCompoundDrawablesWithIntrinsicBounds((leftIcon != 0) ? getColouredIcon(context, leftIcon, color) : null,
            null,
            null,
            (rightIcon != 0) ? getColouredIcon(context, rightIcon, color) : null);
}

public static Drawable getColouredIcon(Context context, int icon, int color) {
    Drawable drawable = VectorDrawableCompat.create(context.getResources(), icon, null);
    drawable = DrawableCompat.wrap(drawable);
    DrawableCompat.setTint(drawable, color);
    return drawable;
}

XML看起来像这样:

<EditText
            android:id="@+id/input_language"
            android:layout_width="@dimen/edittext_width"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:background="@color/bg_edittext"
            android:clickable="true"
            android:cursorVisible="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="@string/hint_language"
            android:inputType="text"
            android:maxLines="1"
            android:padding="10dp"
            android:textColor="@android:color/black"
            android:textColorHint="@color/input_text_hint_bg" />

1 个答案:

答案 0 :(得分:0)

检查文档SetCompundDrawablesWithIntrinsicBounds。 您将可绘制对象设置为底部位置。 更改代码以将可绘制对象置于正确位置

editText.setCompoundDrawablesWithIntrinsicBounds((leftIcon != 0) ? getColouredIcon(context, leftIcon, color) : null,
        null,
        (rightIcon != 0) ? getColouredIcon(context, rightIcon, color) : null),
        null;