以编程方式设置色调后,EditText可绘制隐藏

时间:2018-04-10 05:32:47

标签: android android-layout android-edittext kotlin

我的布局中有两个EditText。我正在使用TextInputLayout并添加drawableLeft,现在我正在尝试更改其色调,这对于用户名字段工作正常,但它隐藏了密码字段的左侧可绘制图标。下面是代码,请指导我做错了什么。感谢

<LinearLayout
    android:layout_width="@dimen/margin_300"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">



<android.support.design.widget.TextInputLayout
                                android:id="@+id/userIDLayout"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_toEndOf="@+id/appCompatImageView4">

                                <android.support.v7.widget.AppCompatEditText
                                    android:id="@+id/edit_user_id"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:drawableLeft="@drawable/ic_user_login"
                                    android:drawablePadding="@dimen/margin_10"
                                    android:drawableStart="@drawable/ic_user_login"
                                    android:drawableTint="@color/colorPrimaryDark"
                                    android:hint="@string/txt_user_hint"
                                    android:imeOptions="flagNoExtractUi|actionNext"
                                    android:inputType="number"
                                    android:maxLines="1"
                                    android:textSize="@dimen/txt_size_16" />
                            </android.support.design.widget.TextInputLayout>


                            <android.support.design.widget.TextInputLayout
                                android:id="@+id/userPasswordLayout"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_toEndOf="@+id/appCompatImageView5"
                                app:passwordToggleEnabled="true">

                                <android.support.design.widget.TextInputEditText
                                    android:id="@+id/edit_user_password"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:drawableLeft="@drawable/ic_user_password"
                                    android:drawablePadding="@dimen/margin_10"
                                    android:drawableStart="@drawable/ic_user_password"
                                    android:drawableTint="@color/colorPrimaryDark"
                                    android:hint="@string/txt_pin_hint"
                                    android:imeOptions="flagNoExtractUi|actionDone"
                                    android:inputType="numberPassword"
                                    android:maxLength="4"
                                    android:maxLines="1"
                                    android:textSize="@dimen/txt_size_16" />
                            </android.support.design.widget.TextInputLayout>

                        </LinearLayout>

以下是我用来以编程方式更改色调的代码:

fun changeEditTextDrawableColor(colorId: Int, drawableId: Int, resource: EditText) {
    var drawable = resources.getDrawable(drawableId)
    drawable = DrawableCompat.wrap(drawable)
    DrawableCompat.setTint(drawable, resources.getColor(colorId))
    DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN)

    resource!!.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
}

1 个答案:

答案 0 :(得分:0)

问题在于setCompoundDrawablesWithIntrinsicBounds将其更改为setCompoundDrawablesRelativeWithIntrinsicBounds并且它将开始显示。