在attrs.xml中,您可以直接在“资源”部分内或在“declare-styleable”中声明自定义属性: 所以现在我们将“attrib1”定义为不风格,“attrib2”为风格。 在layout / someactivity.xml中,我们可以直接使用这些属性(不需要命名空间): 您可以在style.xml声明中使用“styleable”属性“attrib2”。
declare-styleable是给自定义控件添加自定义属性用的 attr中 在attrs.xml中设置declare-styleable,name是PersonAttr <? > <resources> <declare-styleable name="PersonAttr"> <attr name="name" format="reference > </resources> format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有: reference:参考某一资源ID,以此类推 属性定义: <declare-<em>styleable</em> name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable> 属性使用: <ImageView (R.styleable.PersonAttr_age, 15); int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1
我看了一些资料,说写在declare-styleable系统会自动生成数组….. 我不太明白这实际应用是什么? 如果说自动帮你生成了数组,方便使用,那写在外面的三个又有什么作用? attr name=”titleTextColor” format=”color” /> <attr name=”titleTextSize” format=”dimension” /> <declare-styleable ”> <attr name=”titleText” /> <attr name=”titleTextColor” /> <attr name=”titleTextSize” /> </declare-styleable <declare-styleable name=”CustomTitleView”> <attr name=”titleText” format=”string” /> <attr name=”titleTextColor ” format=”color” /> <attr name=”titleTextSize” format=”dimension” /> </declare-styleable> 自定义style 声明和引用的关系
出现 ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute 可以使用cordova plugin add cordova-plugin-file-opener2
declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs.xml <?xml version="1.0" encoding="utf-8"? > <resources> <declare-styleable name="TestAttr"> <attr name="name" format="reference" / ); String name = tArray.getString(R.styleable.TestAttr_name); System.out.println("name = " + name); int age = tArray.getInt(R.styleable.TestAttr_age, 200); System.out.println ("age = " + age); float demin = tArray.getDimension(R.styleable.TestAttr_textSize,0);
> <resources> <declare-styleable name="ExTextView"> <attr name="enableOnPad" format="boolean " /> <attr name="supportDeviceType" format="reference"/> </declare-styleable> <declare-styleable " /> <attr name="supportDeviceType" format="reference"/> </declare-styleable> <declare-styleable .但是当我使用R.styleable.ExEditText_supportDeviceType时候,R文件却没有生成,重新清理了工程还是不生效,不知道是否为adt插件的问题.其他人也遇到了这样的问题. "/> </declare-styleable> </resources> 每次引用attr后,建议清理一下工程,确保R文件重新生成.
format 值类型 reference 资源ID color 颜色值 dimension 尺寸值 float 浮点值 string 字符串 fraction 百分数 <declare-styleable value="0" /> <flag name="stateWindowvisible" value="1" /> </attr> </declare-styleable
); buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5); itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground 该文件是定义属性名和格式的地方,需要用<declare-styleable name=”ToolBar”></declare-styleable>包围所有属性。 1.定义: 1 2 3 <declare-styleable name="My"> <attr name="label" format="reference" > </declare-styleable </declare-styleable> 2.使用: 1 <Button zkx:myWidth="100dip"/> 五、float:浮点型 1.定义: 1 2 3 <declare-styleable " /> </declare-styleable> 2.使用: 1 <rotate zkx:pivotX="200%"/> 八、fraction:百分数 1.定义: 1 2 3 <declare-styleable
1.定义: 1 2 3 <declare-styleable name=”My”> <attr name=”label” format=”reference” > </declare-styleable > </declare-styleable> 2.使用: 1 <Button zkx:isVisible=”false”/> 四、dimension:尺寸值 1.定义: 1 2 3 <declare-styleable ” /> </declare-styleable> 2.使用: 1 <alpha zkx:fromAlpha=”0.3″/> 六、integer:整型 1.定义: 1 2 3 <declare-styleable =”string” /> </declare-styleable> 2.使用: 1 <rotate zkx:pivotX=”200%”/> 八、fraction:百分数 1.定义: 1 2 3 <declare-styleable obtainStyledAttributes(attrs, R.styleable.EasyHeadViewClick) if (attributes !
attr和styleable的关系 首先要明确一点,attr不依赖于styleable,styleable只是为了方便attr的使用。 ,我们可以在R文件里自动生成一个int[],数组里面的int就是定义在styleable里面的attr的id。 所以我们在获取属性的时候就可以直接使用styleable数组来获取一系列的属性。 (set,R.styleable.custom_attrs); 由上面的例子可以知道,定义一个declare-styleable,在获取属性的时候为我们自动提供了一个属性数组。 此外,我觉得使用declare-styleable的方式有利于我们我们把相关的属性组织起来,有一个分组的概念,属性的使用范围更加明确。
最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleable,下面我把自己的使用感受和如何使用进行说明下。 declare-styleable:declare-styleable是给自定义控件添加自定义属性用的。 ); 这个就是系统在默认的资源文件R.styleable中去获取相关的配置。 name = “名称”> <attr name = “textColor” format = “color” /> </declare-styleable> 3. boolean:布尔值 <declare-styleable /> </declare-styleable> 5. float:浮点值。
fillXY" value="0"/> <enum name="center" value="1"/> </attr> //自定义控件的主题样式 <declare-styleable 定义: <declare-styleable name = "名称"> <attr name = "background" format = "reference " /> </declare-styleable> 使用: <ImageView android:layout_width = "42dip" name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable> name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable>
* @attr ref android.R.styleable#RelativeLayout_Layout_layout_toLeftOf * @attr ref android.R.styleable * @attr ref android.R.styleable#RelativeLayout_Layout_layout_below * @attr ref android.R.styleable * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignTop * @attr ref android.R.styleable android.R.styleable#RelativeLayout_Layout_layout_centerInParent * @attr ref android.R.styleable * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignEnd * @attr ref android.R.styleable
--自定义进度条--> <declare-styleable name="TolyProgressBar"> <! (R.styleable.TolyProgressBar_z_pb_on_color, mPbOnColor); mPbTxtSize = (int) a.getDimension(R.styleable.TolyProgressBar_z_pb_txt_size --自定义进度条--> * <declare-styleable name="TolyProgressBar"> * <! , mPbTxtColor); sb.append("m" + s + " = a.getColor(R.styleable." + styleableName + " , mPbTxtColor); sb.append("m" + s + " = a.getBoolean(R.styleable." + styleableName +
, R.styleable.AndroidManifestActivity_label, R.styleable.AndroidManifestActivity_icon , R.styleable.AndroidManifestActivity_roundIcon, R.styleable.AndroidManifestActivity_logo R.styleable.AndroidManifestActivity_process, R.styleable.AndroidManifestActivity_description , R.styleable.AndroidManifestActivity_enabled); } mParseActivityArgs.tag , false); } a.info.theme = sa.getResourceId(R.styleable.AndroidManifestActivity_theme, 0);
R文件中会有styleable和attr这两个类,当我们要使用哪个属性集合或哪个属性的时候用的是styleable, 而attr类定义的仅仅是attr这个属性在layout中的id. (1)属性定义: <declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable (1)属性定义: <declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable (1)属性定义: <declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable (1)属性定义: <declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable
(1)属性定义: <declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable (1)属性定义: <declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable (1)属性定义: <declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable (1)属性定义: <declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable (1)属性定义: <declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable
转载请注明,联系请邮件nlp30508@qq.com 这节主要学习下 R.styleable 这个一般出现在定义的attrs.xml文件中, 命名方式是:<declare-styleable :R.styleable.PieChart_showText This code declares two custom attributes, showText and labelPosition , that belong to a styleable entity named PieChart. 上面的代码定义了两个自定义属性,showText 和labelPosition,他们属于styleable类型的一个名叫PieChart实体类的两个属性。 这里告诉我们,一般我们去定义自己的视图类的时候,类名最好使用 <declare-styleable name="PieChart">中name的定义的名字,与它保持一致。当然它也不是绝对必要的。
); CharSequence s = a.getString(R.styleable.LabelView_text); if (s ! , 0xFF000000)); int textSize = a.getDimensionPixelOffset(R.styleable.LabelView_textSize R.styleable.LabelView 这些内容在 res/values/的 attrs.xml 文件中进行了定义,内容如下所示: <declare-styleable name="LabelView <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable R.styleable.LabelView_text,R.styleable.LabelView_textColor 和 R.styleable.LabelView_textSize 是在源代码中使用的属性
paint.strokeCap = Paint.Cap.ROUND val array = context.obtainStyledAttributes(attrs, R.styleable.CameraRecordButton ) // 背景 background_color = array.getColor( R.styleable.CameraRecordButton_background_color , 270 ) progress_duration = array.getInt( R.styleable.CameraRecordButton_progress_duration progress * 360, false, paint ) } } 这是xml属性文件: <declare-styleable > <attr name="progress_duration" format="integer" tip="进度耗时,单位毫秒" /> </declare-styleable