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.styleable.SegmentedItem_textSelectedColor,Color.WHITE);

        mDescriptionColor = a.getColor(R.styleable.SegmentedItem_descriptionColor,0);
        mDescriptionSelectedColor = a.getColor(R.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值