我的布局中有两个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)
}
问题出在setCompoundDrawablesWithIntrinsicBounds
上,将其更改为setCompoundDrawablesRelativeWithIntrinsicBounds
,它将开始出现。