Android 仿iOS UISegmentedControl

 

完整项目:Github,包括使用说明
https://github.com/CodeManLH/SegmentedControl

先上图

写项目时有个需求需要展示SegmentedControl(分段控制器),iOS是有的,可惜Android没有,本来想网上找一个用的,但是都不符合需求,项目用的是两行,网上大多数都是用RadioGroup,去实现的,这里我用自定义继承LinearLayout 实现,因为是两行没办法,那就自己动手写一个吧,实现不复杂,上代码

先说单个Segmented,定义一个类继承LinearLayout

 

Segmented

public class Segmented extends LinearLayout {

    ...
  // 这里接收定义好的属性,然后给属性赋值

    public Segmented(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SegmentedItem);
        mText = a.getString(R.styleable.SegmentedItem_text);
        mDescription = a.getString(R.styleable.SegmentedItem_description);

  // 接收Dimension时要注意,如果在布局文件定义sp,在setTextSize时,统一把单位换成px,不然字体会很大

        mTextSize = a.getDimension(R.styleable.SegmentedItem_textSize,DensityUtil.toSp(context,12));
        mDescriptionSize = a.getDimension(R.styleable.SegmentedItem_descriptionSize,DensityUtil.toSp(context,12));

        mTextColor = a.getColor(R.styleable.SegmentedItem_textColor,0);
        mTextSelectedColor = a.getColor(R.sty
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可自定义样式、功能全面的分段控件。项目地址:https://github.com/klongmitre/android-segmented-control-view效果图:如何使用xml中直接创建<org.mitre.ascv.AndroidSegmentedControlView         android:id=&quot;@ id/androidSegmentedControlView&quot;         android:layout_width=&quot;match_parent&quot;         android:layout_height=&quot;wrap_content&quot;         ascv:ascv_defaultSelection=&quot;0&quot;         ascv:ascv_unselectedTextColor=&quot;@color/test_attr_unselected_text_color&quot;         ascv:ascv_selectedTextColor=&quot;@color/test_attr_selected_text_color&quot;         ascv:ascv_selectedColor=&quot;@color/test_attr_selected_color&quot;         ascv:ascv_unselectedColor=&quot;@color/test_attr_unselected_color&quot;         ascv:ascv_items=&quot;@array/three_state_option&quot;/>2. java中添加监听器,监听item的切换ascv = (AndroidSegmentedControlView)this.findViewById(R.id.androidSegmentedControlView); ascv.setIdentifier(&quot;ascv01&quot;); //ascv.setItems(new String[]{&quot;Test1aaaaa&quot;, &quot;Test2&quot;, &quot;Test3&quot;}, new String[]{&quot;1&quot;, &quot;2&quot;, &quot;3&quot;}); ascv.setOnSelectionChangedListener(new OnSelectionChangedListener(){     @Override     public void newSelection(String identifier, String value) {//当item切换时触发  Toast.makeText(MainActivity.this, &quot;identifier:&quot; identifier &quot;  value:&quot; value, Toast.LENGTH_SHORT).show();     } });参数identifier是当有多个分段控件时,同时使用一个监听器时,用于区别是哪个触发了事件。属性说明属性名类型使用说明ascv_unselectedTextColorreference未选中的item的文字颜色ascv_unselectedColorreference未选中的item的背景颜色,不包括边框ascv_selectedColorreference选中的item背景的颜色以及边框的颜色ascv_selectedTextColorreference选中的item的文字颜色ascv_itemsreference控件item上显示的文字ascv_valuesreference控件item的值,会被传给监听器。未设置时,默认使用ascv_items。ascv_equalWidthboolean当item上的文字,即ascv_items设置的文字,长度不一致时,item的宽度是否还等长。ascv_stretchboolean是否被拉伸。即控件是否填充整个父容器。ascv_defaultSelectioninteger默认哪个item被选中,下标从0开始ascv_identifierstring控件的ID

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值