Android如何在代码中获取attr属性的值
来源:互联网 发布:js 设置display block 编辑:程序博客网 时间:2024/05/16 03:55
获取arrt的值
有时候我们需要把颜色,数值写成attr属性,这样做是为了屏蔽开发者对应具体数值,比如我们需要设置不同主题下的主色,副色,或者是不同版本的ActionBar大小,亦或者是不同Dpi下的DrawerLayout的宽度等。
在xml里,我们可以简单的引用attr属性值,例如:
android:background="?attr/colorPrimary"android:minHeight="?attr/actionBarSize"
当然,我们有时候也需要在代码中获取attr属性值:
TypedValue typedValue = new TypedValue();context.getTheme().resolveAttribute(R.attr.yourAttr, typedValue, true);// For stringtypedValue.stringtypedValue.coerceToString()// For other datatypedValue.resourceIdtypedValue.data;
获取arrt样式中的值
以上是针对个体数值根据不同类型来获取的,如果想要获取 style
的话,需要在拿到 resourceId
之后再进一步获取具体数值,以 TextAppearance.Large
为例:
<style name="TextAppearance.Large"> <item name="android:textSize">22sp</item> <item name="android:textStyle">normal</item> <item name="android:textColor">?textColorPrimary</item></style>
TypedValue typedValue = new TypedValue();context.getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);int[] attribute = new int[] { android.R.attr.textSize };TypedArray array = context.obtainStyledAttributes(typedValue.resourceId, attribute);int textSize = array.getDimensionPixelSize(0 /* index */, -1 /* default size */);array.recycle();
注意,要记得调用 TypedArray.recycle()
方法回收资源。
最后
看上去挺烦锁的,实际上应该是傻瓜思维,根据不同方法直接获取,例如:
getValueOfColorAttr(int attr)getValueOfTextSizeAttr(int style, int value)
attr 属性
style 样式
二者都是在res/values下面的xml文件
attr: for example:
类似的"layout_height,layout_width"都是属性
style就是这个控件设定好的一些值,方便重复调用
自定义的一个样式"TextStyle"
使用的android默认的属性shadowDx..设置好的具体的值
自定义的attr: 这个format就是格式
reference 表示引用,参考某一资源ID
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算
使用的时候,在布局文件头里
添加
xmlns:myapp="http://schemas.android.com/apk/res/包名"
在xml文件里使用跟系统自带控件一样使用
类似下面的语法
myapp:vpiIconPageIndicatorStyle="xxxxx"
====================================================================================================================================================================
上边的如果要使用自定义的属性进行自定义样式
格式应该是
attr和style格式都是需要<resources>起头的
获取示例:
style 样式
二者都是在res/values下面的xml文件
attr: for example:
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
类似的"layout_height,layout_width"都是属性
style就是这个控件设定好的一些值,方便重复调用
自定义的一个样式"TextStyle"
使用的android默认的属性shadowDx..设置好的具体的值
- <style name="TextStyle">
- <item name="android:shadowDx">-0.5</item>
- <item name="android:shadowDy">1</item>
- <item name="android:shadowRadius">0.5</item>
- <item name="android:singleLine">true</item>
- <item name="android:ellipsize">marquee</item>
- </style>
自定义的attr: 这个format就是格式
reference 表示引用,参考某一资源ID
string 表示字符串
color 表示颜色值
dimension 表示尺寸值
boolean 表示布尔值
integer 表示整型值
float 表示浮点值
fraction 表示百分数
enum 表示枚举值
flag 表示位运算
- <resources>
- <declare-styleable name="ViewPagerIndicator">
- <!-- Style of the circle indicator. -->
- <attr name="vpiCirclePageIndicatorStyle" format="reference"/>
- <!-- Style of the icon indicator's views. -->
- <attr name="vpiIconPageIndicatorStyle" format="reference"/>
- <!-- Style of the line indicator. -->
- <attr name="vpiLinePageIndicatorStyle" format="reference"/>
- <!-- Style of the title indicator. -->
- <attr name="vpiTitlePageIndicatorStyle" format="reference"/>
- <!-- Style of the tab indicator's tabs. -->
- <attr name="vpiTabPageIndicatorStyle" format="reference"/>
- <!-- Style of the underline indicator. -->
- <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>
- </declare-styleable>
- </resources>
使用的时候,在布局文件头里
添加
xmlns:myapp="http://schemas.android.com/apk/res/包名"
在xml文件里使用跟系统自带控件一样使用
类似下面的语法
myapp:vpiIconPageIndicatorStyle="xxxxx"
====================================================================================================================================================================
上边的如果要使用自定义的属性进行自定义样式
格式应该是
- <style name="Theme.PageIndicatorDefaults" parent="android:Theme">
- <item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item>
- <item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item>
- </style>
- //上图样式的名称是"Theme.PageIndicatorDefaults",包含两个属
- //性"vpiIconPageIndicatorStyle"和"vpiTabPageIndicatorStyle",值都是引用类型的
attr和style格式都是需要<resources>起头的
获取示例:
- TypedArray a = context.obtainStyledAttributes(null, R.styleable.ViewPagerIndicator,0, 0);
- int rsid= a.getResourceId(R.styleable.ViewPagerIndicator_vpiTabPageIndicatorStyle, 0);
- a.recycle();//必须回收
0 0
- Android-如何在代码中获取attr属性的值
- Android如何在代码中获取attr属性的值
- Android,如何在代码中获取attr属性的值
- Android,如何在代码中获取attr属性的值
- 如何在代码中获取attr属性的值
- 在Android里,应该如何在代码中获取attr属性的值
- 通过attr获取元素的属性值
- jquery中val获取值,attr获取属性值和attr设置属性值
- jquery中获取属性值的方法attr() prop()的区别
- 关于android view属性的归属以及如何在代码中设置相关属性总结
- 如何在.cs代码中获取textarea标签的值?
- jquery attr()方法 添加,修改,获取对象的属性值。
- jquery attr()方法 添加,修改,获取对象的属性值
- jquery attr()方法 添加,修改,获取对象的属性值。
- jquery attr()方法 添加,修改,获取对象的属性值
- jquery attr()方法 添加,修改,获取对象的属性值。
- jquery attr()方法 添加,修改,获取对象的属性值
- 操纵DOM之---attr 获取与设置属性的值
- 异常(二)
- caffe使用中的问题记录
- PAT 团体程序设计天梯赛-练习集 L2-001. 紧急救援 【dijkstra】
- <spring boot>jsp视图的一个bug
- 【反向拓扑+优先队列】HDU4857逃生【BestCoder Round #1】
- Android如何在代码中获取attr属性的值
- 第十四周项目3—数组类模板
- 设计模式连载:第一篇转职系统模式
- 第十四周项目61-数组类模板
- 设计模式连载:第二篇职业和武器模式,成就你的梦想
- poj 8464 股票买卖(dp/分治)
- NSThread(线程)
- git无法检测到项目更新问题
- android之PendingIntent的使用