Android iOS 7-like UISegmentedControl Segmentation

Source: Internet
Author: User

Android iOS 7-like UISegmentedControl Segmentation

 

Here, only two buttons are simple.

The first is the background of the two buttons:

Res/drawable/seg_left.xml

 

 

Res/drawable/seg_right.xml

 

 

 
      
  
 

Font color:

 

Res/drawable/seg_text_color_selector.xml

 

 
     
  
   
  
 

These are the settings for the selected status.

 

Next we will transform LinearLayout ~~~

In fact, there are two textviews.

SegmentView. java

 

Package cn. haiwan. app. widget; import org. xmlpull. v1.XmlPullParser; import android. r. integer; import android. content. context; import android. content. res. colorStateList; import android. util. attributeSet; import android. util. typedValue; import android. view. gravity; import android. view. view; import android. widget. linearLayout; import android. widget. textView; import cn. haiwan. r; public class SegmentView extends Lin EarLayout {private TextView textView1; private TextView textView2; private onSegmentViewClickListener listener; public SegmentView (Context context, AttributeSet attrs) {super (context, attrs); init ();} public SegmentView (Context context) {super (context); init ();} private void init () {// this. setLayoutParams (new LinearLayout. layoutParams (dp2Px (getContext (), 60), LinearLayout. layoutParams. WRAP_CONTENT); text View1 = new TextView (getContext (); textView2 = new TextView (getContext (); textView1.setLayoutParams (new LayoutParams (0, LayoutParams. WRAP_CONTENT, 1); textView2.setLayoutParams (new LayoutParams (0, LayoutParams. WRAP_CONTENT, 1); textView1.setText (SEG1); textView2.setText (SEG2); XmlPullParser xrp = getResources (). getXml (R. drawable. seg_text_color_selector); try {ColorStateList csl = ColorStateList. CRES AteFromXml (getResources (), xrp); textView1.setTextColor (csl); textView2.setTextColor (csl);} catch (Exception e) {} textView1.setGravity (Gravity. CENTER); textView2.setGravity (Gravity. CENTER); textView1.setPadding (3, 6, 3, 6); textView2.setPadding (3, 6, 3, 6); setSegmentTextSize (16); textView1.setBackgroundResource (R. drawable. seg_left); textView2.setBackgroundResource (R. drawable. seg_right); textView1. SetSelected (true); this. removeAllViews (); this. addView (textView1); this. addView (textView2); this. invalidate (); expire (new OnClickListener () {@ Overridepublic void onClick (View v) {if (textView1.isSelected () {return;} textView1.setSelected (true); textView2.setSelected (false ); if (listener! = Null) {listener. onSegmentViewClick (textView1, 0) ;}}); textView2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (textView2.isSelected () {return ;} textView2.setSelected (true); textView1.setSelected (false); if (listener! = Null) {listener. onSegmentViewClick (textView2, 1) ;}});}/*** set the font size unit dip *

July 18, 2014

* @ Param dp * @ author RANDY. ZHANG */public void setSegmentTextSize (int dp) {textView1.setTextSize (TypedValue. COMPLEX_UNIT_DIP, dp); textView2.setTextSize (TypedValue. COMPLEX_UNIT_DIP, dp);} private static int dp2Px (Context context, float dp) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dp * scale + 0.5f);} public void setOnSegmentViewClickListener (onSegmentViewClickListener listener) {this. listener = listener;}/*** set text *

July 18, 2014

* @ Param text * @ param position * @ author RANDY. ZHANG */public void setSegmentText (CharSequence text, int position) {if (position = 0) {textView1.setText (text);} if (position = 1) {textView2.setText (text) ;}} public static interface onSegmentViewClickListener {/****

July 18, 2014

* @ Param v * @ param position 0-left 1-right * @ author RANDY. ZHANG */public void onSegmentViewClick (View v, int position );}}

 

 

Layout file reference

 

 
    
  
 


 

 

 

 

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.