【问题标题】:TextInputLayout set color of startIconDrawable programmaticalyTextInputLayout 以编程方式设置 statIc Drawable 的颜色
【发布时间】:2020-01-13 22:55:42
【问题描述】:

我想以编程方式为我的TextInputLayout 设置开始图标,但我遇到了他的颜色问题。当我设置drawable时,它的颜色变成灰色,但它的原始颜色是橙色。我知道我可以通过在 xml 中使用 startIconTint 参数来更改它的颜色,但我想以编程方式更改它的颜色。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: android material-design android-textinputlayout material-components-android material-components


    【解决方案1】:

    只需使用setStartIconDrawablesetStartIconTintList的方法:

    textInputLayout.setStartIconDrawable(...);
    textInputLayout.setStartIconTintList(ContextCompat.getColorStateList(this,R.color.text_input_selector));
    

    您可以使用颜色或颜色选择器。
    比如:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:color="?attr/colorPrimary" android:state_activated="true"/>
      <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
      <item android:alpha="0.54" android:color="?attr/colorOnSurface"/>
    </selector>
    

    【讨论】:

    • 你能解释一下如何使用setStartIconTintList()
    • @AssetBekbossynov 此方法将色调应用于可绘制的开始图标,如 xml 中的 app:startIconTint 或样式中的 &lt;item name="startIconTint"&gt;....&lt;/item&gt;。您可以使用单一颜色,也可以使用ColorStateList 来定义不同状态下的不同颜色。
    • 谢谢您的回答。我使用ColorStateListvalueOf(color: Int) 方法解决了它。最终版本看起来像textInputLayout.setStartIconTintList(ColorStateList.valueOf(resources.getColor(R.color.color_name)));
    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2018-11-03
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    相关资源
    最近更新 更多