自定义属性 declare-styleable数据类型简介:

自定义属性 declare-styleable数据类型简介:一、reference:参考指定Theme中资源ID。1.定义: 1 2 3 <declare-styleablename=”My”> <attrname=”label”format=”reference”> </declare-styleable> 2.使用: 1…

大家好,又见面了,我是你们的朋友全栈君。

一、reference:参考指定Theme中资源ID。

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”label” format=”reference” >

</declare-styleable>

2.使用:

1

<Buttonzkx:label=”@string/label” >

二、Color:颜色

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”textColor” format=”color” />

</declare-styleable>

2.使用:

1

<Button zkx:textColor=”#ff0000″/>

三、boolean:布尔值

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”isVisible” format=”boolean” />

</declare-styleable>

2.使用:

1

<Button zkx:isVisible=”false”/>

四、dimension:尺寸值

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”myWidth” format=”dimension” />

</declare-styleable>

2.使用:

1

<Button zkx:myWidth=”100dip”/>

五、float:浮点型

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”fromAlpha” format=”float” />

</declare-styleable>

2.使用:

1

<alpha zkx:fromAlpha=”0.3″/>

六、integer:整型

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”frameDuration” format=”integer” />

</declare-styleable>

2.使用:

1

<animated-rotate zkx:framesCount=”22″/>

七、string:字符串

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”Name” format=”string” />

</declare-styleable>

2.使用:

1

<rotate zkx:pivotX=”200%”/>

八、fraction:百分数

1.定义:

1

2

3

<declare-styleable name=”My”>

<attr name=”pivotX” format=”fraction” />

</declare-styleable>

2.使用:

1

<rotate zkx:Name=”My name is zhang kun xiang”/>

九、enum:枚举

1.定义:

1

2

3

4

5

<declare-styleable name=”My”>

<attr name=”language”>

<enum name=”English” value=”1″/>

</attr>

</declare-styleable>

2.使用:

1

<Button zkx:language=”English”/>

十、flag:位或运算

1.定义:

1

2

3

4

5

6

<declare-styleable name=”My”>

<attr name=”windowSoftInputMode”>

<flag name=”stateUnspecified” value=”1″ />

<flag name = “adjustNothing” value = “0x30” />

</attr>

</declare-styleable>

2.使用:

1

<activity android:windowSoftInputMode=”stateUnspecified | adjustNothing”>

属性定义时可以指定多种类型值:

1

2

3

<declare-styleable name = “名称”>

<attr name=”background” format=”reference|color” />

</declare-styleable>

使用:

1

<ImageView android:background = “@drawable/图片ID|#00FF00″/>

例子和使用

class EasyHeadViewClick : LinearLayout {
    private lateinit var mContentView: View
    private lateinit var mllHeadBottomView: LinearLayout
    private var headNum = 2
    private var headOneName = ""
    private var headTwoName = ""
    private var headThreeName = ""
    private var headFourName = ""
    private var mCLickPosition = 0
    private var listName = mutableListOf<String>()
    //默认背景
    private var mCLickBg = R.drawable.shape_stroke_orange_semicircle
    private var mNoCLickBg = R.drawable.shape_stroke_grey_semicircle

    constructor(context: Context?) : super(context) {
        init(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)

    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        mContentView = LayoutInflater.from(context).inflate(R.layout.head_bottom_view, this, false)
        mllHeadBottomView = mContentView.findViewById(R.id.mllHeadBottomView)
        if (attrs != null) {
            val attributes = context?.obtainStyledAttributes(attrs, R.styleable.EasyHeadViewClick)
            if (attributes != null) {
                for (index in 0 until attributes.indexCount) {
                    when (attributes.getIndex(index)) {
                        R.styleable.EasyHeadViewClick_head_num -> {
                            headNum = attributes.getInt(R.styleable.EasyHeadViewClick_head_num, 2)
                        }
                        R.styleable.EasyHeadViewClick_head_one_name -> {
                            headOneName = attributes.getString(R.styleable.EasyHeadViewClick_head_one_name)
                            listName.add(0, headOneName)
                        }
                        R.styleable.EasyHeadViewClick_head_two_name -> {
                            headTwoName = attributes.getString(R.styleable.EasyHeadViewClick_head_two_name)
                            listName.add(1, headTwoName)
                        }
                        R.styleable.EasyHeadViewClick_head_three_name -> {
                            headThreeName = attributes.getString(R.styleable.EasyHeadViewClick_head_three_name)
                            listName.add(2, headThreeName)
                        }
                        R.styleable.EasyHeadViewClick_head_four_name -> {
                            headFourName = attributes.getString(R.styleable.EasyHeadViewClick_head_four_name)
                            listName.add(3, headFourName)
                        }
                        R.styleable.EasyHeadViewClick_head_click_bg -> {
                            mCLickBg = attributes.getResourceId(R.styleable.EasyHeadViewClick_head_click_bg, R.drawable.shape_stroke_orange_semicircle)
                        }
                        R.styleable.EasyHeadViewClick_head_no_click_bg -> {
                            mNoCLickBg = attributes.getResourceId(R.styleable.EasyHeadViewClick_head_no_click_bg, R.drawable.shape_stroke_grey_semicircle)
                        }
                    }
                }
                if (headNum > 0) {
                    for (i in 0 until headNum) {
                        var view = LayoutInflater.from(context).inflate(R.layout.layout_head_view_click_item, this, false)
                        var textView = view.findViewById<TextView>(R.id.tv_head_view_click_item)
                        var mLayoutParams = LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f) //动态设置权重分配
                        mLayoutParams.leftMargin = DensityUtil.px2dp(8).toInt()
                        textView.layoutParams = mLayoutParams
                        textView.text = listName[i]
                        if (i == 0) {
                            textView.setBackgroundResource(mCLickBg)
                            textView.setTextColor(resources.getColor(R.color.colorAccent))
                        } else {
                            textView.setBackgroundResource(mNoCLickBg)
                            textView.setTextColor(resources.getColor(R.color.black_353535))
                        }
                        mllHeadBottomView?.addView(view)
                    }
                }
                attributes.recycle()
            }
        }
        initClick()
        /**
         * 执行才能展示
         */
        addView(mContentView)
    }

    private fun initClick() {
        for (i in 0 until headNum) {
            mllHeadBottomView?.getChildAt(i)?.setOnClickListener { setPayType(i) }
        }

    }

    private fun setPayType(payType: Int) {
        for (i in 0 until headNum) {
            (mllHeadBottomView?.getChildAt(i) as TextView)?.setBackgroundResource(R.drawable.shape_stroke_grey_semicircle)
            (mllHeadBottomView?.getChildAt(i) as TextView)?.setTextColor(UIUtil.getColor(R.color.black_353535))
        }
        (mllHeadBottomView?.getChildAt(payType) as TextView)?.setBackgroundResource(R.drawable.shape_stroke_orange_semicircle)
        (mllHeadBottomView?.getChildAt(payType) as TextView)?.setTextColor(UIUtil.getColor(R.color.colorAccent))
        mCLickPosition = payType + 1//位置从零开始 要加1
        mClickListener?.invoke(mCLickPosition.toString())
    }

    /**
     * 通过接口获取点击位置
     */
    private var mClickListener: ((mCLickPosition: String) -> Unit)? = null

    fun setClickViewListener(mCLickPosition: ((mCLickPosition: String) -> Unit)) {
        mClickListener = mCLickPosition
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159158.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • dropout层

    dropout层之前了解的网络都是在全连接层加dropout层,防止模型过拟合。在看deepfm的模型的时候,发现在每一层都可以加入dropout层对dropout层的深入理解做分类的时候,Dropout层一般加在全连接层防止过拟合提升模型泛化能力。而很少见到卷积层后接Dropout(原因主要是卷积参数少,不易过拟合)er类似卷积神经网络,只是在最后的全连接层使用dropout,循环神经…

    2022年6月10日
    63
  • Anycast 公网加速 AIA解决方案

    Anycast 公网加速 AIA解决方案Anycast公网加速AIA简介Anycast公网加速(AnycastInternetAcceleration,AIA)是一个覆盖多地的动态加速网络,可以大幅提升您业务的公网访问体验。不同于其他应用层加速服务,AIA能实现IP传输的质量优化和多入口就近接入,减少网络传输的抖动、丢包,最终提升云上应用的服务质量,扩大服务范围,精简后端部署。Anycast公网加速AIA功能Anycast公网加速提供多种强大功能,提升应用访问体验的同时,易于部署和管理。1、公网IP任播购买

    2022年5月23日
    123
  • git强制删除本地分支_Git分支

    git强制删除本地分支_Git分支删除分支命令删除一条分支:gitbranch-DbranchName删除当前分支外的所有分支:gitbranch|xargsgitbranch-d删除分支名包含指定字符的分支:gitbranch|grep‘dev*’|xargsgitbranch-d命令解释|道命令,用于将一串命令串联起来。前面命令的输出可以作为后面命令的输入。gitbranch用于列出本地所有分支。xargsxargs是给命令传递参数.

    2022年10月16日
    2
  • 圆柱体的立方米怎么计算公式_圆锥体积计算公式

    圆柱体的立方米怎么计算公式_圆锥体积计算公式2020-11-27石雕圆柱制作价格一般根据石雕圆柱的雕刻材料不同、颜色不同、大小及工艺的复杂程度价格区间一般在5000–25000元每根。坐落于福建省泉州市惠安县的腾磊石材有限公司是石材圆柱厂家,专业生产石雕圆柱。提供各式石雕圆柱加工,客户可来电方式咨询关于石雕圆柱。我国的石雕圆柱文化除了具有图腾性质和纪念性的华表、墓表…[详细]2020-12-14厂家加工石雕圆柱坐落于福建省泉州市惠安县…

    2022年9月20日
    3
  • Hsql函数上

    Hsql函数上Hsql函数.上(关系/数学/逻辑/数值/日期/条件/字符串/集合统计/复杂类型)原文链接:https://blog.csdn.net/scgaliguodong123_/article/details/60881166hive常见函数准备数据zhangsa dfsadsa323 new 67.1 2lisi 543gfd old 43.32 1wanger 65ghf new…

    2025年12月1日
    4
  • tail命令用法举例

    tail命令用法举例tail命令从指定点开始将文件写到标准输出.tail-ffilename可以方便的查阅正在改变的日志文件,会把filename里最新的内容显示在屏幕上1.命令格式:tail[必要参数][选择参数][文件]  2.命令功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。3.命令参数:-f 循环读取-q 不显示文件名-v 显示文件…

    2022年6月4日
    70

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号