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

发布时间:2025-12-09 14:10:06 浏览次数:11

一、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    }}

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477