Android Imitation Apple segmented button

Source: Internet
Author: User

It's just a simple two button.

The first is the background of the two buttons:

Res/drawable/seg_left.xml

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_selected=" true ">        <shape >            <stroke android:color=" # 0079FF "android:width=" 1DP "/>            <solid android:color=" #0079FF "/>            <corners Android:topleftradius = "3DP" android:bottomleftradius= "3DP" android:toprightradius= "0DP" android:bottomrightradius= "0DP"/>        </ shape>    </item>    <item>        <shape >            <stroke android:color= "#0079FF" Android: Width= "1DP"/>            <solid android:color= "#FFFFFF"/>            <corners android:topleftradius= "3DP" Android : bottomleftradius= "3DP" android:toprightradius= "0DP" android:bottomrightradius= "0DP"/>        </shape>    </item></selector>

Res/drawable/seg_right.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_selected=" true ">        <shape >            <stroke android:color=" # 0079FF "android:width=" 1DP "/>            <solid android:color=" #0079FF "/>            <corners Android:topleftradius = "0DP" android:bottomleftradius= "0DP" android:toprightradius= "3DP" android:bottomrightradius= "3DP"/>        </ shape>    </item>    <item>        <shape >            <stroke android:color= "#0079FF" Android: Width= "1DP"/>            <solid android:color= "#FFFFFF"/>            <corners android:topleftradius= "0DP" Android : bottomleftradius= "0DP" android:toprightradius= "3DP" android:bottomrightradius= "3DP"/>        </shape>    </item></selector>
Font Color:

Res/drawable/seg_text_color_selector.xml


<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_selected=" true "android:color=" #FFFFFF "/><item android:color=" # 0079FF "/></selector>


These are the settings for the selected state.

The following linearlayout to transform ~ ~ ~

is actually put two textview.

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 LinearLayout {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)); textView1 = 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.createfromxml (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 (+); 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 (); Textview1.setonclicklistener (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 Font size Unit dip * <p>2014 July 18 </p> * @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 (). Densi Ty;return (int) (DP * scale + 0.5f);} public void Setonsegmentviewclicklistener (Onsegmentviewclicklistener listener) {This.listener = listener;} /** * Set Text * <p>2014 July 18 </p> * @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{/** * * <p>2014 year July 18 </p> * @param v * @param position 0-left Side 1-Right * @author RANDY. ZHANG */public void Onsegmentviewclick (View v,int position);}


Layout file Reference
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >   <cn.haiwan.app.widget.segmentview     android:layout_width= "160DP"    android:layout_height= "Wrap_content"    android:layout_gravity= "Center_horizontal"    /></linearlayout >


For more Andrid tutorials, please visit the Great God Web Android Tutorial

Android Imitation Apple segmented button

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.