The Code sets Shape and Selector. The Code shapeselector
Shape and Selector are often used during development. If each object is set in xml, the apk size will be occupied and the name will be messy. It is much easier to set it using code.
Two classes are required: GradientDrawable and StateListDrawable.
Related APIs:
SetColor (rgb); // fill color
SetGradientType (GradientDrawable. RECTANGLE); // shape the RECTANGLE.
SetCornerRadius (radius); // the radius of the rounded corner.
SetStroke (1, strokenColor); // Border thickness and color
The following provides a tool class and code
[Tool class DrawableUtils ]:
Public class DrawableUtils {
Public static GradientDrawable getDrawable (int rgb, float radius ){
GradientDrawable gradientDrawable = new GradientDrawable ();
GradientDrawable. setColor (rgb); // sets the color.
GradientDrawable. setGradientType (GradientDrawable. RECTANGLE); // you can specify a display style.
GradientDrawable. setCornerRadius (radius); // you can specify the radius of the rounded corner.
GradientDrawable. setStroke (DensityUtil. dip2px (1), rgb); // stroke
Return gradientDrawable;
}
Public static StateListDrawable getSelector (Drawable normalDrawable, Drawable pressDrawable ){
StateListDrawable stateListDrawable = new StateListDrawable ();
// Add the selected image pointing to the status to the current color selector.
StateListDrawable. addState (new int [] {android. R. attr. state_enabled, android. R. attr. state_pressed}, pressDrawable );
StateListDrawable. addState (new int [] {android. R. attr. state_enabled}, normalDrawable );
// Set the default status
StateListDrawable. addState (new int [] {}, normalDrawable );
Return stateListDrawable;
}
}
[Use ]:
Public void initData (String content ){
// Randomly disrupt the datas order
Collections. shuffle (Arrays. asList (datas ));
Random = new Random ();
// Dynamically create TextView
For (int I = 0; I <datas. length; I ++ ){
TextView textView = new TextView (getActivity ());
TextView. setText (datas [I]);
// [Set attributes of textView] ====================================== ==================
// Font size
TextView. setTextSize (DensityUtil. dip2px (10 ));
// Padding
Int padding = DensityUtil. dip2px (3 );
TextView. setPadding (padding, padding );
// Margin
ViewGroup. MarginLayoutParams params = new ViewGroup. MarginLayoutParams (ViewGroup. LayoutParams. WRAP_CONTENT, ViewGroup. LayoutParams. WRAP_CONTENT );
Params. leftMargin = DensityUtil. dip2px (8 );
Params. topMargin = DensityUtil. dip2px (8 );
Params. rightMargin = DensityUtil. dip2px (8 );
Params. bottomMargin = DensityUtil. dip2px (8 );
TextView. setLayoutParams (params );
// Background color
Int red = random. nextInt (211 );
Int green = random. nextInt (211 );
Int blue = random. nextInt (211 );
Int rgb = Color. rgb (red, green, blue );
// Set the background Method 1:
// TextView. setBackgroundColor (rgb );
// Set the background Method 2:
Float radius = DensityUtil. dip2px (5 );
// TextView. setBackground (DrawableUtils. getDrawable (rgb, radius ));
// [Set the click event or selector of textView] ==========================
// Set Selector
GradientDrawable normalDrawable = DrawableUtils. getDrawable (rgb, radius );
GradientDrawable pressDrawable = DrawableUtils. getDrawable (Color. WHITE, radius );
StateListDrawable selector = DrawableUtils. getSelector (normalDrawable, pressDrawable );
TextView. setBackground (selector );
// Set click response
// TextView. setClickable (true );
TextView. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (getActivity (), (TextView) v). getText (). toString (), Toast. LENGTH_SHORT). show ();
}
});
// [Add textView to FramLayout] ======================== ========
FlowLayout. addView (textView );
}
}