Android-自定义视图文字颜色选择器

时间:2020-02-04 12:01:28

标签: android android-view

我创建了一个自定义视图,其中包含TextView,有时需要更改文本颜色。

将标准declare-styleable<attr name="android:textColor"/>一起使用是可行的。

  <declare-styleable name="CustomStatusButton">
    <attr name="android:text"/>
    <attr name="android:textColor"/>
    <attr name="android:enabled"/>
  </declare-styleable>

我用它来设置颜色:

  val textColor = typedArray.getColor(R.styleable. CustomStatusButton_android_textColor, Color.BLACK)
  tv_button_text.setTextColor(textColor)

-

现在,我需要为启用/禁用状态设置选择器颜色。所以我创建了这个:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/red" android:state_enabled="true" />
  <item android:color="@color/green" android:state_enabled="false" />
</selector>

  <CustomStatusButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="SAVE"
      android:textColor="@drawable/button_text_color_selector"
      />

但这不会根据启用/禁用设置颜色。它只会尝到红色的第一色。

我想念什么吗?

1 个答案:

答案 0 :(得分:0)

此代码中的问题

val textColor = typedArray.getColor(R.styleable.CustomStatusButton_android_textColor, Color.BLACK)

您尝试获取颜色,但需要获取ColorStateList

相关问题