//SegmentedGroup类:importandroid.content.Context;importandroid.content.res.ColorStateList;importandroid.content.res.Resources;importandroid.content.res.TypedArray;importandroid.graphics.Color;importandroid.graphics.drawable.Drawable;importandroid.graphics.drawable.GradientDrawable;importandroid.graphics.drawable.StateListDrawable;importandroid.os.Build;importandroid.util.AttributeSet;importandroid.util.TypedValue;importandroid.view.View;importandroid.widget.Button;importandroid.widget.LinearLayout;importandroid.widget.RadioGroup;importcom.uwo.tools.R;publicclassSegmentedGroupextendsRadioGroup{privateintmMarginDp;privateResourcesresources;privateintmTintColor;privateintmCheckedTextColor=Color.WHITE;privateLayoutSelectormLayoutSelector;privateFloatmCornerRadius;publicSegmentedGroup(Contextcontext){super(context);resources=getResources();mTintColor=resources.getColor(R.color.radio_button_selected_color);mMarginDp=(int)getResources().getDimension(R.dimen.radio_button_stroke_border);mCornerRadius=getResources().getDimension(R.dimen.radio_button_conner_radius);mLayoutSelector=newLayoutSelector(mCornerRadius);}/*Readstheattributesfromthelayout*/privatevoidinitAttrs(AttributeSetattrs){TypedArraytypedArray=getContext().getTheme().obtainStyledAttributes(attrs,R.styleable.SegmentedGroup,0,0);try{mMarginDp=(int)typedArray.getDimension(R.styleable.SegmentedGroup_sc_border_width,getResources().getDimension(R.dimen.radio_button_stroke_border));mCornerRadius=typedArray.getDimension(R.styleable.SegmentedGroup_sc_corner_radius,getResources().getDimension(R.dimen.radio_button_conner_radius));mTintColor=typedArray.getColor(R.styleable.SegmentedGroup_sc_tint_color,getResources().getColor(R.color.radio_button_selected_color));mCheckedTextColor=typedArray.getColor(R.styleable.SegmentedGroup_sc_checked_text_color,getResources().getColor(android.R.color.white));}finally{typedArray.recycle();}}publicSegmentedGroup(Contextcontext,AttributeSetattrs){super(context,attrs);resources=getResources();mTintColor=resources.getColor(R.color.radio_button_selected_color);mMarginDp=(int)getResources().getDimension(R.dimen.radio_button_stroke_border);mCornerRadius=getResources().getDimension(R.dimen.radio_button_conner_radius);initAttrs(attrs);mLayoutSelector=newLayoutSelector(mCornerRadius);}@OverrideprotectedvoidonFinishInflate(){super.onFinishInflate();//UsehololightfordefaultupdateBackground();}publicvoidsetTintColor(inttintColor){mTintColor=tintColor;updateBackground();}publicvoidsetTintColor(inttintColor,intcheckedTextColor){mTintColor=tintColor;mCheckedTextColor=checkedTextColor;updateBackground();}publicvoidupdateBackground(){intcount=super.getChildCount();for(inti=0;i<count;i++){Viewchild=getChildAt(i);updateBackground(child);//Ifthisisthelastview,don'tsetLayoutParamsif(i==count-1)break;LayoutParamsinitParams=(LayoutParams)child.getLayoutParams();LayoutParamsparams=newLayoutParams(initParams.width,initParams.height,initParams.weight);//Checkorientationforpropermarginsif(getOrientation()==LinearLayout.HORIZONTAL){params.setMargins(0,0,-mMarginDp,0);}else{params.setMargins(0,0,0,-mMarginDp);}child.setLayoutParams(params);}}privatevoidupdateBackground(Viewview){intchecked=mLayoutSelector.getSelected();intunchecked=mLayoutSelector.getUnselected();//SettextcolorColorStateListcolorStateList=newColorStateList(newint[][]{{android.R.attr.state_pressed},{-android.R.attr.state_pressed,-android.R.attr.state_checked},{-android.R.attr.state_pressed,android.R.attr.state_checked}},newint[]{Color.GRAY,mTintColor,mCheckedTextColor});((Button)view).setTextColor(colorStateList);//RedrawwithtintcolorDrawablecheckedDrawable=resources.getDrawable(checked).mutate();DrawableuncheckedDrawable=resources.getDrawable(unchecked).mutate();((GradientDrawable)checkedDrawable).setColor(mTintColor);((GradientDrawable)checkedDrawable).setStroke(mMarginDp,mTintColor);((GradientDrawable)uncheckedDrawable).setStroke(mMarginDp,mTintColor);//Setproperradius((GradientDrawable)checkedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));((GradientDrawable)uncheckedDrawable).setCornerRadii(mLayoutSelector.getChildRadii(view));//CreatedrawableStateListDrawablestateListDrawable=newStateListDrawable();stateListDrawable.addState(newint[]{-android.R.attr.state_checked},uncheckedDrawable);stateListDrawable.addState(newint[]{android.R.attr.state_checked},checkedDrawable);//Setbuttonbackgroundif(Build.VERSION.SDK_INT>=16){view.setBackground(stateListDrawable);}else{view.setBackgroundDrawable(stateListDrawable);}}/**Thisclassisusedtoprovidetheproperlayoutbasedontheview.*Alsoprovidestheproperradiusforcorners.*Thelayoutisthesameforeachselectedleft/topmiddleorright/bottombutton.*floattablesforsettingtheradiusviaGradient.setCornerRadiiareusedinstead*ofmultiplexmldrawables.*/privateclassLayoutSelector{privateintchildren;privateintchild;privatefinalintSELECTED_LAYOUT=R.drawable.radio_checked;privatefinalintUNSELECTED_LAYOUT=R.drawable.radio_unchecked;privatefloatr;//thisistheradiosreadbyattributesorxmldimensprivatefinalfloatr1=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,0.1f,getResources().getDisplayMetrics());//0.1dptopxprivatefinalfloat[]rLeft;//leftradiobuttonprivatefinalfloat[]rRight;//rightradiobuttonprivatefinalfloat[]rMiddle;//middleradiobuttonprivatefinalfloat[]rDefault;//defaultradiobuttonprivatefinalfloat[]rTop;//topradiobuttonprivatefinalfloat[]rBot;//botradiobuttonprivatefloat[]radii;//resultradiifloattablepublicLayoutSelector(floatcornerRadius){children=-1;//InitthistoforcesetChildRadii()toenterforthefirsttime.child=-1;//InitthistoforcesetChildRadii()toenterforthefirsttimer=cornerRadius;rLeft=newfloat[]{r,r,r1,r1,r1,r1,r,r};rRight=newfloat[]{r1,r1,r,r,r,r,r1,r1};rMiddle=newfloat[]{r1,r1,r1,r1,r1,r1,r1,r1};rDefault=newfloat[]{r,r,r,r,r,r,r,r};rTop=newfloat[]{r,r,r,r,r1,r1,r1,r1};rBot=newfloat[]{r1,r1,r1,r1,r,r,r,r};}privateintgetChildren(){returnSegmentedGroup.this.getChildCount();}privateintgetChildIndex(Viewview){returnSegmentedGroup.this.indexOfChild(view);}privatevoidsetChildRadii(intnewChildren,intnewChild){//Ifsamevaluesarepassed,justreturn.Noneedtoupdateanythingif(children==newChildren&&child==newChild)return;//Setthenewvalueschildren=newChildren;child=newChild;//ifthereisonlyonechildprovidethedefaultradiobuttonif(children==1){radii=rDefault;}elseif(child==0){//leftortopradii=(getOrientation()==LinearLayout.HORIZONTAL)?rLeft:rTop;}elseif(child==children-1){//rightorbottomradii=(getOrientation()==LinearLayout.HORIZONTAL)?rRight:rBot;}else{//middleradii=rMiddle;}}/*Returnstheselectedlayoutidbasedonview*/publicintgetSelected(){returnSELECTED_LAYOUT;}/*Returnstheunselectedlayoutidbasedonview*/publicintgetUnselected(){returnUNSELECTED_LAYOUT;}/*ReturnstheradiifloattablebasedonviewforGradient.setRadii()*/publicfloat[]getChildRadii(Viewview){intnewChildren=getChildren();intnewChild=getChildIndex(view);setChildRadii(newChildren,newChild);returnradii;}}}

颜色值:

<colorname="radio_button_selected_color">#ff33b5e5</color><colorname="radio_button_unselected_color">@android:color/transparent</color>

attrs:

<declare-styleablename="SegmentedGroup"><attrname="sc_corner_radius"format="dimension"/><attrname="sc_border_width"format="dimension"/><attrname="sc_tint_color"format="color"/><attrname="sc_checked_text_color"format="color"/></declare-styleable>

dimen:

<dimenname="radio_button_conner_radius">5dp</dimen><dimenname="radio_button_stroke_border">1dp</dimen>

style:

<stylename="RadioButton"><itemname="android:textColor">@drawable/button_text_color</item><itemname="android:minHeight">33dp</item><itemname="android:minWidth">70dp</item><itemname="android:gravity">center</item><itemname="android:button">@null</item><itemname="android:paddingLeft">5dp</item><itemname="android:paddingRight">5dp</item></style>

button_text_color:

<?xmlversion="1.0"encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:color="#DDd4d4d4"/><itemandroid:state_pressed="false"android:state_checked="false"android:color="@color/radio_button_selected_color"/><itemandroid:state_pressed="false"android:state_checked="true"android:color="#fff2f2f2"/></selector>

radio_checked:

<?xmlversion="1.0"encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:padding="10dp"><!--youcanuseanycoloryouwantIusedheregraycolor--><solidandroid:color="@color/radio_button_selected_color"/><strokeandroid:width="@dimen/radio_button_stroke_border"android:color="@color/radio_button_selected_color"/></shape>

radio_unchecked:

<?xmlversion="1.0"encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:padding="10dp"><!--youcanuseanycoloryouwantIusedheregraycolor--><solidandroid:color="@color/radio_button_unselected_color"/><strokeandroid:width="@dimen/radio_button_stroke_border"android:color="@color/radio_button_selected_color"/></shape>

github例子位置:

https://github.com/hoang8f/android-segmented-control


更多相关文章

  1. Android通过PopupMenu定义弹出菜单的位置
  2. android 数据库更改数据库位置【DbFlow示范】
  3. Android自定义对话框(Dialog)位置,大小
  4. Android(安卓)各种Dialog例子
  5. Android(安卓)实现旋转键盘的例子
  6. Android:实现两个数相加的例子
  7. android sdk 更新速度慢的解决办法
  8. Android中的Selector的使用
  9. 2018-07-23

随机推荐

  1. Python入门:函数加括号和不加括号的区别
  2. 与kubectl exec运行后台进程
  3. 人工智能和机器学习领域开源项目
  4. python练习_购物车(2)
  5. Python 爬取CSDN博客频道
  6. 谈网页游戏外挂之用python模拟游戏(热血
  7. 使用芹菜接收未注册的任务
  8. 【Python截图】截图处理
  9. 自学Python九 爬虫实战二(美图福利)
  10. ubuntu使用uwsgi+nginx部署django