2017-09-04 35 views
2

我正在使用TextInput布局创建文本输入框。我想根据输入框的不同变体应用drawable和color资源。 我已经在res/color和res/drawable目录下创建了不同的xml资源文件。setTextColor等同于TextInputLayout

public enum InputTextVariant { 
    Standard, Stepper, MultiLine; 
} 
public void setVariant(int variantParam) { 
     Drawable d; 
     ColorStateList csl; 
     InputTextVariant variant = SpectrumInputTextVariant.values()[variantParam]; 
     switch (variant) { 
      case Standard: 
       csl = AppCompatResources.getColorStateList(getContext(), R.color.textcolor_btn_cta); 
       d = AppCompatResources.getDrawable(getContext(), R.drawable.btn_cta_material); 
       //setTextColor(csl); 
       setBackgroundTintList(csl); 
       setBackground(d); 

我想使用类似setTextColor的按钮。 我为不同的状态指定了不同的颜色和形状(禁用,悬停,聚焦等)。 如何加载此TextInputLayout的颜色资源。 我试图setBackgroundTint需要API版本> = 21。但我还需要支持较低版本。

回答

0

您可以在绘制级别管理色彩:

Drawable d = AppCompatResources.getDrawable(...); 
ColorStateList csl = AppCompatResources.getColorStateList(...); 
d = DrawableCompat.wrap(d); 
DrawableCompat.setTintList(csl); 
setBackground(d);