JAVA code sets the background color of the selector in different states

  • Code to implement Shape

  • Code to implement Selector

  • The use of StateListDrawable and GradientDrawable

In Android development, we often use custom drawable styles to set shape styles or selector selectors in drawables, but sometimes if multiple drawable styles are needed in an xml, and the styles set inside are only subtle If the difference changes, then customizing multiple drawable styles will be bloated, making the drawable xml files too many and management troublesome, so sometimes this star thinks that it is possible to create drawables in the code.

The use of StateListDrawable and GradientDrawable

java implements selector:

/**
  * set background selector
  *
  * @param pressedDraw
  * @param normalDraw
  * @return
*/
private StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw) {
        StateListDrawable stateListDrawable = new StateListDrawable ();
        stateListDrawable.addState(new int[]{android.R.attr.state_selected}, pressedDraw);
        stateListDrawable.addState(new int[]{}, normalDraw);
        return stateListDrawable;
}

  This is to create a selector in the code. The created type is StateListDrawable. You can add states to the selector through addState(), but it should be noted that when adding states, there is an order. StateListDrawable will execute the latest added state first. If it is not the state , When executing the following state, if a large range of state is added to the front, it will cause the large range of state to be directly executed without executing the following state. In addition, in adding state, add a "-" sign before the state, indicating that this state is false (for example: -android.R.attr.state_selected), otherwise it is true.

/**
  * set shape
  *
  * @param radius
  * @param fillColor
  * @param width
  * @param strokeColor
  * @return
 */
private GradientDrawable getDrawable(int radius, int fillColor, int width, int strokeColor) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setCornerRadius(radius);
    gradientDrawable.setColor(fillColor);
    gradientDrawable.setStroke(width, strokeColor);
    return gradientDrawable;
}

  Dynamically create a GradientDrawable in the code, which can realize the function of the shape style in the drawable style. setColor is equal to the fill color in the shape. setCornerRadius is to set the corner radius in the shape. If you want to set the radian of a single corner, you can use setCornerRadii() to set each The radian of a corner, setStroke() is the stroke, you need to fill in the width of the stroke and the color of the side.

Finally simple use:

GradientDrawable normal = getDrawable(0, Color.WHITE, 1, getResources().getColor(R.color.app_line_color));

GradientDrawable press = getDrawable(0, Color.WHITE, 1, getResources().getColor(Config.currentThemeColorId));

StateListDrawable selector = getSelector(normal, press);
textView.setBackground(selector);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388378&siteId=291194637