The Code sets Shape and Selector. The Code shapeselector

Source: Internet
Author: User

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.