[Original] custom control Androidsegmentcontrolview, imitation iOS platform Uisegmentcontrolview, inherit from view

Source: Internet
Author: User

Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

Introduction to Controls

Uisegmentcontrol is very common in apps on the iOS platform, and its controls are as follows:

The main function of this kind of control is to dynamically change the display content of the interface, generally applied to the interface with more content, and split screen display different kinds of content.

In the Android development process, sometimes need to achieve similar uisegmentcontrol effect, here I open my Code on GitHub, named Segmentcontrolview, the following is the effect of the implementation:

The project address can be clicked on the following link:
Https://github.com/Carbs0126/AndroidSegmentControlView

Control description
1 extends View

This Segmentcontrolview is inherited from the view, not the composite control, so the implementation is a bit cumbersome, but the flexibility is much higher than the combined control, and can be combined with excessive effects, and more than the composite control is more lightweight.

Control features

The Segmentcontrolview I realized has the following characteristics:

  1.SegmentControlView可设置与ViewPager联动,在segment切换时具有颜色渐变效果,类似。  2.可以自定义SegmentControlView的四个corner的半径。  3.自定义背景颜色与字体颜色、字体大小等等。  4.在xml中设置SegmentControlView中的多个titles,自动按照titles生成多个segment。  5.设置按下颜色与normal颜色的色值对比度。  6.具有AT_MOST的适配功能,即具有wrap_content模式  7.可以设置回调响应事件
Using the method Properties file description
<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <declare-styleablename= "Segmentcontrolview">        <attrname= "Scv_backgroundselectedcolor"format= "Reference|color" />//Check the background color of segment<attrname= "Scv_backgroundnormalcolor"format= "Reference|color" />//Segment background Color not selected<attrname= "Scv_textselectedcolor"format= "Reference|color" />//Check the text color of segment<attrname= "Scv_textnormalcolor"format= "Reference|color" />//Segment text color not selected<attrname= "Scv_framecolor"format= "Reference|color" />//segment The color of the border<attrname= "Scv_framewidth"format= "Reference|dimension" />//segment width of border<attrname= "Scv_framecornerradius"format= "Reference|dimension" />//segment radius size of four rounded corners<attrname= "Scv_textsize"format= "Reference|dimension" />//Text size<attrname= "Scv_textarray"format= "Reference" />//string Array, each string will be populated into a segment<attrname= "Scv_selectedindex"format= "Reference|integer" />//Segment selected by default<attrname= "Scv_segmentpaddinghorizontal"format= "Reference|dimension" />//Each segment internal level padding<attrname= "Scv_segmentpaddingvertical"format= "Reference|dimension" />The vertical direction of each segment padding<attrname= "Scv_gradient"format= "Reference|boolean" />//segment whether to use a color gradient effect when changing</declare-styleable></Resources>

Layout file Creation Segmentcontrolview
<Cn.carbs.android.segmentcontrolview.library.SegmentControlViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_horizontal"Android:paddingleft= "10DP"Android:paddingright= "10DP"App:scv_framecornerradius= "6DP"App:scv_framewidth= "1DP"app:scv_gradient= "true"app:scv_segmentpaddingvertical= "5DP"App:scv_textarray= "@array/segment_control_arrays_0"/>

How to use

dependencies {    compile ' cn.carbs.android:segmentcontrolview:1.0.0 '}
Segmentcontrolview.setonsegmentchangedlistener (NewSegmentcontrolview.onsegmentchangedlistener () {@Override Public voidOnsegmentchanged (intNewselectedindex) {            if(Viewpager! =NULL){                //Change the second argument to True if you want the gradient effect when Viewpager is changingViewpager.setcurrentitem (Newselectedindex,false);//Viewpager changing without animation            }        }    }); //set Viewpager to change segment according to the state of ViewpagerSegmentcontrolview.setviewpager (Viewpager); //Set the selected index of segments initiativelySegmentcontrolview.setselectedindex (); //set Gradient effect if you wantSegmentcontrolview.setgradient (true);
Project Address:

Https://github.com/Carbs0126/AndroidSegmentControlView

Git:

git clone https://github.com/Carbs0126/AndroidSegmentControlView.git

[Original] custom control Androidsegmentcontrolview, imitation iOS platform Uisegmentcontrolview, inherit from view

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.