自定义属性 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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python3整数反转

    python3整数反转

    2021年4月18日
    183
  • 远程开机(外网WOL远程唤醒)「建议收藏」

    远程开机(外网WOL远程唤醒)「建议收藏」Win10开启网络唤醒功能的操作方法:PS:远程唤醒的要求1.首先,我们要在主板BIOS里面设置WOL唤醒功能的开关,大部分主板都会支持唤醒2.电脑的主板和网卡需要支持网络唤醒。一般无线网卡是不支持的,板载的有线网卡一般是可以的。3.所在网络环境需要有公网IP。如果是ADSL拨号的话,如果获取的是私网地址的话,那可以向运营商申请公网IP。4.主机跟路由器要保证一直通电,…

    2022年6月2日
    50
  • 清华大学团队:人脸识别爆出巨大丑闻,15分钟解锁19款手机

    清华大学团队:人脸识别爆出巨大丑闻,15分钟解锁19款手机1清华大学的RealAI团队15分钟解锁19款手机刚刚,清华大学的一条重大发现,利用人脸识别技术的漏洞,“15分钟解锁19个陌生智能国产手机”的事件,引发无数网友关注。据悉,清华大学的RealAI团队共选取了20款手机,其中1款是国外的,另外19款都是我们国产的智能手机,居来自排名前五的国产手机品牌,每一品牌下选取了3-4款不同价位的手机型号,覆盖低端机到旗舰机。1)测试步骤如下:第一步,清华大学的测试人员,把19部国产手机,人脸识别全部绑定为旁边的“1号同学”;

    2022年6月5日
    139
  • formData原生实现图片上传

    formData原生实现图片上传

    2022年2月11日
    49
  • 软件开发视频资源分享[通俗易懂]

    软件开发视频资源分享[通俗易懂]资源分享资源均来源于网络,在自学/开公众号的时候收集而来。如果侵权请联系我,会第一时间删除。如果链接已失效(我也无办法,很多链接我是没有保存在自已的网盘中的,见谅)。Java基础Java马士兵:链接:https://pan.baidu.com/s/1jJRvxGi密码:v3xbJava毕向东:可以到往B站找,已上传到B站Java刘意:链接:https://pan.bai…

    2022年5月21日
    45
  • 安卓rom包制作_android原生rom下载

    安卓rom包制作_android原生rom下载制作ROM包是做Android系统工程师的第一步,Android第三方的市场

    2022年10月16日
    0

发表回复

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

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