天天看点

android declare-styleable 和style,Android 关于declare-styleable属性的写法….

大家好,又见面了,我是你们的朋友全栈君。

我想问自定义View的时候,以下这段代码,为何要写两次一样的名称呢?

我看了一些资料,说写在declare-styleable系统会自动生成数组…..

我不太明白这实际应用是什么?

如果说自动帮你生成了数组,方便使用,那写在外面的三个又有什么作用?

能从实际应用中讲一讲吗?

<?xml version=”1.0″ encoding=”utf-8″?>

<resources>

<attr name=”titleText” format=”string” />

<attr name=”titleTextColor” format=”color” />

<attr name=”titleTextSize” format=”dimension” />

<declare-styleable name=”CustomTitleView”>

<attr name=”titleText” />

<attr name=”titleTextColor” />

<attr name=”titleTextSize” />

</declare-styleable>

</resources>

=========================================

那可以简写把?

<declare-styleable name=”CustomTitleView”>

<attr name=”titleText” format=”string” />

<attr name=”titleTextColor” format=”color” />

<attr name=”titleTextSize” format=”dimension” />

</declare-styleable>

自定义style

声明和引用的关系,外面的三个是声明了三个属性的名称及可赋值类型,CustomTitleView则是引用了三个外面声明的属性,表明了CustomTitleView下有三个属性。

应用时,比如在style.xml下引用到了CustomTitleView这个style,就可以对三个属性赋值,生成特定的style

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/158841.html原文链接:https://javaforall.cn