How to find out if android supports: textColor is int or ColorStateList

In the user view group, I have a TextView as a child. I want to set the TextView textColor based on android: value to textColor. So in res / values ​​/styles.xml I have:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="CustomViewGroupTextView">
    <attr name="android:textColor" />
  </declare-styleable>
</resources>

And in the CustomViewGroup constructor, I have the following:

private TextView mTextView;    

public CustomViewGroup(Context context) {
  super(context);
  initTextView(context, attrs);
}

public CustomViewGroup(Context context, AttributeSet attrs) {
  super(context, attrs);
  initTextView(context, attrs);
}

public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  initTextView(context, attrs);
}

private void initTextView(Context context, AttributeSet attrs) {
  mTextView = new TextView(
  TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomViewGroupTextView);

  // Set text color
  ColorStateList textColor = ta.getColorStateList(R.styleable.MinutiaeTextView_android_textColor);
  if (textColor != null) {
    mTextView.setTextColor(textColor);
  }
}

My question is: how to execute mTextView.setTextColor correctly? Anyone can specify a complete list of color states or a single color value in android: textColor. Or will I get a ColorStateList with all one color if someone puts one color in android: textColor?

+4
source share
2 answers

:

, ColorStateList

, , , ColorStateList.

+1

TextView:

mTextView.setTextColor(Color.RED); (RED, WHITE, BLACK....)

mTextView.setTextColor(Color.rgb(200,0,0));

mTextView.setTextColor(getResources().getColor(R.color.yourcolor));

mTextView.setTextColor(0xAARRGGBB);

EDIT: XML TextView:

android:textColor="Here"

:

android:color/white (black, red...)

color/yourcolorname

#738184

Etc...

0

Source: https://habr.com/ru/post/1539484/


All Articles