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

相关推荐

  • Android Hook技术实践

    Android Hook技术实践一、hook简介hook俗称钩子,主要作用是替换系统内存中的对象,在上层调用此对象的方法的时候可以修改传入参数或者返回值,达到欺骗上层的目的,就像小红帽故事里的大灰狼,通过扮演小红帽的外婆的角色来达到欺骗小红帽的目的。其实hook就是一种中间人劫持的思想,如图所示:在安卓中实现hook主要通过两种方式:1.反射技术和代理实现,当然代理不管是动态还是静态的都是可以实现的,但是只能ho

    2022年5月26日
  • 【100个 Unity踩坑小知识点】| Unity调用API ,动态获取Android权限,附带所有Android权限表格

    【100个 Unity踩坑小知识点】| Unity调用API ,动态获取Android权限,附带所有Android权限表格Unity小科普老规矩,先介绍一下Unity的科普小知识:Unity是实时3D互动内容创作和运营平台。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意变成现实。Unity平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。也可以简单把Unity理解为一个游戏引擎,可以用来专业制作游戏!Unity踩坑小知识点学习Unity调用AP..

    2022年7月17日
  • 如何用c语言调用c++做成的动态链接库

    今天在做东西的时候遇到一个问题,就是如何在C语言中调用C++做的动态链接库so文件如果你有一个c++做的动态链接库.so文件,而你只有一些相关类的声明,那么你如何用c调用呢,别着急,本文通过一个小小

    2021年12月23日
  • 浮动广告代码实例「建议收藏」

    浮动广告代码实例「建议收藏」很多网站的页面都有漂浮的广告效果,虽然烦人,但也确实起到了良好的宣传效果。各大代码网站也有关于漂浮代码的实例,很多存在着兼容性问题,不符合W3C标准,本站修复了兼容性问题,下面就简单介绍一下如何实现此效果。代码如下:[HTML]纯文本查看复制代码运行代码0102030405060708091011121314151617181920212223242526272829303

    2022年9月20日
  • mycat oracle读写分离_mycat读写分离原理

    mycat oracle读写分离_mycat读写分离原理今天做不成的事,明天也不会做好。各位同学大家好,今天给大家分享一下用Mycat进行数据库的读写分离,本篇文章是基于上一篇的mysql主从复制。Linux上实现Mysql的主从复制(为Mycat读写分离作准备)在上一篇文章中,我们在两个服务器使用同版本的操作系统和mysql:服务器1:centos7.3,mysql5.6服务器2:centos7.3,m…

    2022年10月13日
  • 计算机考试身份证和准考证号

    计算机考试身份证和准考证号大家好啊,距离22考研初试仅剩26天,现在这个时候,大家除了对知识点进行查缺补漏之外,也得关注一些关于考前的准备工作,还有考场的注意事项哦!因为地区的不同,考点的不同,监考老师的不同,考试的要求和规定也会有差异哦,所以大家在拿到准考证之后,一定要仔细查看考试考点的要求~#考研倒计时#打开腾讯新闻,查看更多图片>1、可不可以戴手表、手环?如果要戴手表,那必须是没有记忆及计算功能的,也就是机械表,手环算电子产品大概率是不可以带的,在进考场之前可以询问一下监考老师,确认一下是否可以带

    2022年5月26日

发表回复

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

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