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


相关推荐

  • 2021年最新Java学习路线图指南

    2021年最新Java学习路线图指南Java在编程语言排行榜中一直牢牢占据榜首位置,几乎所有的大中型互联网的应用系统在服务器端开发首选都是Java编程,正因如何吸引这不少年轻人投入该行业,Java虽不想其它编程语言那么复杂,但是知识体系还是很庞大的,因此想要学好并非容易之事,不少想要跨入Java编程行业的同学们通过网络搜索各式各样的学习资料,却往往缺乏系统而全面的学习路线。动力节点深知同学们的学习困难,为此整理了一套最新的2021年新版学习路线图,增加了目前企业最新应用技术,这套学习路线图,只要你完成一半基本就可以找到很不错的…

    2022年5月17日
    42
  • Struts的ONGL

    Struts的ONGL

    2022年1月15日
    49
  • smarty怎么用_item怎么用

    smarty怎么用_item怎么用1、简介含义:Smarty是PHP的一个引擎模板,可以更好的进行逻辑与显示的分离,即我们常说的MVC,这个引擎的作用就是将C分离出来。环境需求:PHP5.2或者更高版本我使用的环境是:PHP5.

    2022年8月5日
    6
  • php中的ceil和floo以及round函数「建议收藏」

    php中的ceil和floo以及round函数「建议收藏」php中的ceil和floo以及round函数

    2022年4月21日
    51
  • 在 Win10 系统下安装 JDK 及配置环境变量的方法

    在 Win10 系统下安装 JDK 及配置环境变量的方法首先,在官网下载JDK:Oracle官网如上图所示,在Oracle官网下载JDK,但有一点需要注意,那就是在咱们下载合适的JDK之前,需要先点击“标记1”所在的按钮,选择接受。否则的话,直接点击JDK进行下载的时候,会弹出出现界面:选择接受“AcceptLicenseAgreement”之后,再点击JDK进行下载就会弹出下载提示框了,如下图所示:下载完成后,双击可执行文件,

    2022年7月24日
    6
  • Python 贪吃蛇 代码

    Python 贪吃蛇 代码#!/usr/bin/envpython#-*-coding:utf-8-*-importpygameaspygameimportrandomimportsysfrompygame.rectimportRectclassSnake(object):def__init__(self):#定义3个颜色属性,以防后面需要self.black=pygame.Color(0,0,0)#背景颜色self

    2022年5月26日
    40

发表回复

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

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