In Android development, we often use custom drawable styles, setting shape styles or selector selectors in drawables, but sometimes if multiple drawable styles are needed in one XML, and the styles set in them are only slightly different, then customizing multiple drawable styles will appear bloated, making the drawable too many xml files, which is troublesome to manage, so sometimes I think that I can create drawables in the code.
The use of StateListDrawable and GradientDrawable
Java implementation 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 type created is StateListDrawable. You can add state to the selector through addState(). However, it should be noted that when adding state, there is an order. StateListDrawable will first execute the latest added state. If it is not the state, execute the following state. If a large-scale state is placed in front, it will directly execute the large-scale state without executing the subsequent state. In addition, when adding state, add the "-" sign before 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 GradientDrawable in the code, which can realize the function of shape style in drawable style. SetColor is equal to the fill color in shape. SetCornerRadius is to set the radian of the rounded corner in shape. If you want to set the radian of a single corner, you can use setCornerRadii() to set the radian of each corner. SetStroke() is a stroke, and you need to fill in the width of the stroke and the color of the edge.
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);
Summarize
The above is the JAVA code introduced by the editor to set the background color of the selector in different states. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!