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


相关推荐

  • java ee简介_Java EE 简介

    java ee简介_Java EE 简介JavaEE简介JavaEE基本架构JavaEE的诞生是为了解决传统C/S架构的弊端:客户端臃肿庞大,扩展性差等弊端。JavaEE将传统的两层结构细分为了四层。这四层分别是:Client层,Web层,Business层,EIS层JavaEE客户端JavaEE客户端层可分为以下几种:Web客户端,通常是浏览器呈现的网页,这种客户端成为轻量级客户端,因为他们不与…

    2022年7月7日
    19
  • android之layout布局和ListView中的一些属性介绍[通俗易懂]

    一.android:singleLine设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text=”test_ singleLine “android:singleLine=”true”android:layout_width=”20dp”将只显示”t…”如果不设置singleLine或者设置为false

    2022年3月10日
    45
  • 从U盘安装centos7(史上最简单的终极解决方案)

    从U盘安装centos7(史上最简单的终极解决方案)1、下载最新版本的centos(目前是centos7.6)https://www.centos.org/download/由于我是安装服务器,所以选择了MinimalISO(只有900多MB)2、制作Centos安装盘(准备一个空白的1GB以上的U盘)2.1.安装UltraISO2.2.安装完成后点…

    2022年5月20日
    52
  • Academic social networks: Modeling, analysis, mining and applications 2019翻译[通俗易懂]

    Academic social networks: Modeling, analysis, mining and applications 2019翻译[通俗易懂]Academicsocialnetworks:Modeling,analysis,miningandapplications摘要:在快速增长的学术大数据背景下,社交网络技术最近引起了学术界和工业界的广泛关注。学术社会网络的概念正是在学术大数据的背景下产生的,指的是由学术实体及其关系形成的复杂的学术网络。有大量的学术大数据处理方法来分析学术社交网络丰富的结构类型和相关信息。现在各种学术数据都很容易获取,这让我们更容易分析和研究学术社交网络。本研究调查了学术社交网络的背景、现状和趋势。我们首先

    2022年6月1日
    31
  • ue4 插件开发(ue4性能优化)

    UE4插件研发UE4插件扫盲UE4插件是什么UE4插件的作用UE4引擎、项目、插件的区别引擎自带的插件浏览器插件目录结构插件配置文件说明UE4插件的创建方法插件代码的执行过程四种常用模式的插件模板第三方库的引入牛刀小试创建Actor到视图插件插件发布常见的问题插件项目案例UE4插件扫盲UE4插件是什么UE4引擎和UE4项目是由各个模块组成的,其主要编程语言是C++。插件也是一个模块,说到插…

    2022年4月14日
    200
  • js什么是匿名函数_js函数返回值

    js什么是匿名函数_js函数返回值js匿名函数的代码如下:(function(){ // 这里忽略jQuery 所有实现 })();半年前初次接触jQuery 的时候,我也像其他人一样很兴奋地想看看源码是什么样的。然而,在看到源码的第一眼,我就迷糊了。为什么只有一个匿名函数又没看到运行(当然是运行了…… ),就能有jQuery 这么个函数库了?于是,我抱着疑问来到CSDN 。结果相信现在很多人都很清楚了(因为在

    2022年9月27日
    4

发表回复

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

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