android 自定义步骤栏

简介: 今天自定义了一个操作步骤栏,虽然在平常的项目中用处不大,但是也值得记录一下,先看下效果图

今天自定义了一个操作步骤栏,虽然在平常的项目中用处不大,但是也值得记录一下,先看下效果图
image.png

图中红圈部分的内容就是效果图

先看下使用方法,代码使用了kotlin语言,如果是要java代码的话,可以自行用去转化,不难。

1.布局中使用自定义控件
image.png

2.在activity或是Fragment中使用
image.png

可以看到,使用了建造者模式提供了很多方法供用户自己设置属性,用户可以设置步骤总数,当前处于哪个步骤,是否显示间隔线以及间隔线的宽度高度颜色等,备注里写的很清楚。

以下是完整自定义组件的完整代码

class RegisterStepsBar : LinearLayout {
    private var mCurrentBackground: Int = R.drawable.shape_circle_step //当前圆圈背景色
    private var mPassedBackground: Int = R.drawable.shape_circle_white //通过的圆圈背景色
    private var mNormalBackground: Int = R.drawable.shape_circle_gray //未通过的圆圈背景色
    private var mSpaceBetween: Int = 25 //间隔
    private var mNormalWidth: Int = 14 //未通过的宽度
    private var mCurrentWidth: Int = 18 //当前的宽度
    private var mCurrentStep: Int = 0 //默认当前步骤下标为0
    private var mStepCount: Int = 3 //默认总共3个步骤
    private var mLineWidth: Int = 20 //间隔线宽度 默认是20
    private var mLineHeight: Int = 1 //间隔线高度 默认是1
    private var isShow: Boolean = true //是否显示分隔线
    private var currentTextColor: Int = 0 //当前步骤的字体颜色
    private var passTextColor: Int = 0 //通过的步骤的字体颜色
    private var normalTextColor: Int = 0 //未通过的步骤的字体颜色
    private var lineColor: Int = 0  //间隔线颜色
    private var textSize: Float = 13f //步骤的字体颜色
    private var contentText = emptyArray<String>() //设置文本内容
 
    private var lineView: View? = null
    private var tvCurrentStep: TextView? = null
 
    constructor(context: Context?) : super(context)
 
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
 
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
 
    /**
     *  dp转换为px
     */
    private fun dp2px(dp: Float): Int {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics).toInt()
    }
 
    /**
     * 初始化控件,先布局后动态创建组件
     */
    fun init() {
        if (mStepCount <= 0)
            return
        if (childCount > 0)
            removeAllViews()
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
        for (item: Int in 0 until mStepCount) {
            tvCurrentStep = TextView(context)
            if (item < mStepCount - 1 && isShow) {//创建间隔线
                lineView = View(context)
                lineView!!.setBackgroundResource(lineColor)
                val params1 = LayoutParams(dp2px(mLineWidth.toFloat()), dp2px(mLineHeight.toFloat()))
                params1.gravity = Gravity.CENTER
                params1.leftMargin = mSpaceBetween
                lineView!!.layoutParams = params1
            }
            var background: Int
            var width: Int = mNormalWidth
            when {
                item < mCurrentStep -> {
                    background = mPassedBackground
                    tvCurrentStep!!.setTextColor(context.resources.getColor(passTextColor))
                }
                item == mCurrentStep -> {
                    background = mCurrentBackground
                    width = mCurrentWidth
                    tvCurrentStep!!.setTextColor(context.resources.getColor(currentTextColor))
                }
                else -> {
                    background = mNormalBackground
                    tvCurrentStep!!.setTextColor(context.resources.getColor(normalTextColor))
                }
            }
            tvCurrentStep!!.gravity = Gravity.CENTER
            tvCurrentStep!!.textSize = textSize
            if (contentText.isNotEmpty())
                tvCurrentStep!!.text = contentText[item]
            else
                tvCurrentStep!!.text = (item + 1).toString()
 
            val params = LayoutParams(width, width)
            if (item > 0)
                params.leftMargin = mSpaceBetween
            tvCurrentStep!!.layoutParams = params
            addView(tvCurrentStep)
            if (lineView != null) {
                addView(lineView)
                lineView = null
            }
            if (tvCurrentStep is ViewGroup)
                tvCurrentStep = getChildAt(0) as TextView
            tvCurrentStep!!.setBackgroundResource(background)
        }
    }
 
    /**
     * 从Builder中获取到数据
     */
    fun setData(barBuilder: BarBuilder) {
        this.mCurrentBackground = barBuilder.mCurrentBackground
        this.mPassedBackground = barBuilder.mPassedBackground
        this.mNormalBackground = barBuilder.mNormalBackground
        this.mSpaceBetween = barBuilder.mSpaceBetween
        this.mNormalWidth = barBuilder.mNormalWidth
        this.mCurrentWidth = barBuilder.mCurrentWidth
        this.mCurrentStep = barBuilder.mCurrentStep
        this.mStepCount = barBuilder.mStepCount
        this.mLineWidth = barBuilder.mLineWidth
        this.mLineHeight = barBuilder.mLineHeight
        this.isShow = barBuilder.isShow
        this.lineColor = barBuilder.lineColor
        this.currentTextColor = barBuilder.currentTextColor
        this.passTextColor = barBuilder.passTextColor
        this.normalTextColor = barBuilder.normalTextColor
        this.textSize = barBuilder.textSize
        this.contentText = barBuilder.contentText
    }
 
    /**
     * 建造者模式类 ---------------------------------------------------------------------------
     */
    inner class BarBuilder {
        var mCurrentBackground: Int = R.drawable.shape_circle_step //当前圆圈背景色
        var mPassedBackground: Int = R.drawable.shape_circle_white //通过的圆圈背景色
        var mNormalBackground: Int = R.drawable.shape_circle_gray //未通过的圆圈背景色
        var mSpaceBetween: Int = 25 //间隔
        var mNormalWidth: Int = 36 //未通过的宽度
        var mCurrentWidth: Int = 38 //当前的宽度
        var mCurrentStep: Int = 0 //默认当前步骤下标为0
        var mStepCount: Int = 3 //默认总共3个步骤
        var mLineWidth: Int = 20 //间隔线宽度 默认是20
        var mLineHeight: Int = 1 //间隔线高度 默认是1
        var isShow: Boolean = true //是否显示分隔线
        var lineColor: Int = R.color.colorPrimary //间隔线颜色
        var currentTextColor: Int = R.color.colorPrimary //当前步骤的字体颜色
        var passTextColor: Int = R.color.colorPrimary //通过的步骤的字体颜色
        var normalTextColor: Int = R.color.colorPrimary //未通过的步骤的字体颜色
        var textSize: Float = 13f //步骤的字体颜色
        var contentText = emptyArray<String>() //设置文本内容
 
        /**
         * 动态设置多少步骤
         */
        fun setUp(stepCount: Int, currentStep: Int): BarBuilder {
            this.mStepCount = stepCount
            if (currentStep > 0)
                this.mCurrentStep = currentStep - 1
            return this
        }
 
        /**
         * 设置分割线的宽度
         */
        fun setLineWidth(width: Float, height: Float): BarBuilder {
            mLineWidth = dp2px(width)
            mLineHeight = dp2px(height)
            return this
        }
 
        fun setTextSize(size: Float): BarBuilder {
            this.textSize = size
            return this
        }
 
        /**
         * 设置圆圈背景色
         */
        fun setBackground(currentBg: Int, passedBg: Int, normalBg: Int): BarBuilder {
            mCurrentBackground = currentBg
            mPassedBackground = passedBg
            mNormalBackground = normalBg
            return this
        }
 
        /**
         * 设置圆圈的大小
         */
        fun setCircleWidth(currentWidth: Int, normalWidth: Int): BarBuilder {
            mCurrentWidth = currentWidth //当前步骤圆圈大小
            mNormalWidth = normalWidth //非当前步骤圆圈大小
            return this
        }
 
        /**
         * 是否显示间隔线
         */
        fun isShowLine(isShow: Boolean): BarBuilder {
            this.isShow = isShow
            return this
        }
 
        /**
         * 设置间隔线的颜色
         */
        fun setLineColor(color: Int): BarBuilder {
            this.lineColor = color
            return this
        }
 
        /**
         * 设置步骤文本内容
         */
        fun setContent(content: Array<String>): BarBuilder {
            if (content.size != mStepCount)
                return this
            else
                contentText = content
            return this
        }
 
        /**
         * 设置圆圈跟分隔线的间距或是圆圈跟圆圈之间的间距
         */
        fun setSpace(space: Int): BarBuilder {
            mSpaceBetween = space
            return this
        }
 
        /**
         * 设置字体的颜色
         */
        fun setTextColor(currentTextColor: Int, passTextColor: Int, normalTextColor: Int): BarBuilder {
            this.currentTextColor = currentTextColor
            this.passTextColor = passTextColor
            this.normalTextColor = normalTextColor
            return this
        }
 
        fun build(): RegisterStepsBar {
            this@RegisterStepsBar.setData(this)
            return this@RegisterStepsBar
        }
    }
}
相关文章
|
15天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
48 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
7月前
|
API Android开发
Android 自定义最大宽度,高度, 宽高比例 Layout
Android 自定义最大宽度,高度, 宽高比例 Layout
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
4月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
34 1
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
20天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
11 0
|
7月前
|
XML Android开发 数据格式
自定义Android titleBar
自定义Android titleBar
26 0
|
8月前
|
Android开发
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
Android 使用ViewPager和自定义PagerAdapter实现轮播图效果
70 0