仿IOS分段控件SegmentConrtol

该篇文章从eoeAndroid搬迁过来的,原文地址:[android实例]仿ios分段控件SegmentConrtol

前段时间由于公司需求,我学习了一段时间的MONO for ios的开发,并动手做了两个不完整的项目,由于我是搞android的,对于ios和C#都不太懂,所以面对mono for ios有点吃力,各种迷茫,各种吃力,不过在学习mono for ios过程中我也收获颇多,现在回想起来还是感觉挺庆幸的有这种机会的。

在学习mono for ios中,对于ios的一些控件有很深的印象,特别是现在很多应用都模仿ios的一写程序和控件,就此我也写了一个关于ios的分段控件segmentcontrol,方便以后在android项目上用到。

这个控件我是用button做的,根据设置的内容不同可以自动生成几段。

主要代码:

首先是得到自定义的属性值,其中的没段内容是根据buttonContent 得来的,buttoncontent里的内容要用分号(;)隔开,如上图的(中国:美国:英国)

public SegmentButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.SegmentButton);
        textPressColor = typedArray.getColor(
                R.styleable.SegmentButton_textPressColor, Color.WHITE);
        textNormalColor = typedArray.getColor(
                R.styleable.SegmentButton_textNormalColor,
                Color.rgb(0, 122, 255));
        textSize = typedArray.getFloat(R.styleable.SegmentButton_textSize, 16);
        buttonContent = typedArray
                .getString(R.styleable.SegmentButton_buttonContent);
        if (buttonContent != null && (!"".equals(buttonContent))) {
            contentStr = buttonContent.split(";");
            if (contentStr != null) {
                buttonCount = contentStr.length;
            } else {
                buttonCount = 0;
            }
        } else {
            buttonCount = 0;
        }
        setView();
    }

其次是根据buttoncontent内容分隔的长度new出对应的button,并加载在布局中

private void setView() {
        buttonList = new ArrayList<Button>();
        if (buttonCount == 1) {
            Button button = new Button(context);
            button.setBackgroundDrawable(context.getResources().getDrawable(
                    R.drawable.segment_sigle));
            button.setTextSize(textSize);
            button.setTextColor(textPressColor);
            button.setGravity(Gravity.CENTER);
            button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT));
            button.setTag(0);
            this.addView(button);
            button.setOnClickListener(this);
            buttonList.add(button);
        } else {
            for (int i = 0; i < buttonCount; i++) {
                Button button = new Button(context);
                if (i == 0) {
                    button.setBackgroundDrawable(context.getResources()
                            .getDrawable(R.drawable.segment_left_press));
                    button.setTextColor(textPressColor);
                } else if (i == buttonCount - 1) {
                    button.setBackgroundDrawable(context.getResources()
                            .getDrawable(R.drawable.segment_right_normal));
                    button.setTextColor(textNormalColor);
                } else {
                    button.setBackgroundDrawable(context.getResources()
                            .getDrawable(R.drawable.segment_middle_normal));
                    button.setTextColor(textNormalColor);
                }
                button.setTextSize(textSize);
                button.setGravity(Gravity.CENTER);
                button.setLayoutParams(new LinearLayout.LayoutParams(0,
                        LayoutParams.MATCH_PARENT, 1));
                button.setTag(i);
                this.addView(button);
                button.setOnClickListener(this);
                buttonList.add(button);
            }
        }

        for (int i = 0; i < buttonCount; i++) {
            buttonList.get(i).setText(contentStr[i]);
        }
    }

我们在布局中使用这个控件:

xmlns:attr="http://schemas.android.com/apk/res/com.example.segmentbutton"

    <com.example.segmentbutton.SegmentButton
        android:id="@+id/segment_button"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        attr:buttonContent="中国;美国;英国;"
        attr:textSize="20" />

该控件的点击事件是:

segment_button.setOnCheckedChangeListener(new SegmentButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(int position, Button button) {
                        // TODO Auto-generated method stub
                        // textView.setText(segment_button.getSegmentButton(position).getText()
                        // .toString());
                        textView.setText(button.getText().toString());
                    }

                });
segment_button.getSegmentButton(0)是得到第几个button

下载地址:项目代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值