使用和获取Android的主题属性
2018-01-24 12:02
459 查看
参考文章:
Android,如何在代码中获取attr属性的值
- colorBackGround:activity默认背景
- listChoiceBackgroundIndicator:listView中item使用的背景selector
这些在layout文件中使用时很简单的,只需要
所以我更倾向于使用obtainStyledAttributes方法,比较直观。并且在自定义View中也经常使用。
中,演示了如何获取某个Style中的值。
例如获取TextAppearance.Large中的textSize。
获取方式如下,因为方式1无法直接获取到某个Style中的值,所以两种方式结合:
Android,如何在代码中获取attr属性的值
1. layout布局文件中使用主题属性
很多时候,我们想引用主题中的属性(attr)。例如- colorBackGround:activity默认背景
- listChoiceBackgroundIndicator:listView中item使用的背景selector
这些在layout文件中使用时很简单的,只需要
?+ 属性的方式即可引用。例如
android:background="?android:colorBackground"
2. 代码中获取主题属性的两种方式
2.1 方式1:obtainStyledAttributes(返回TypedArray)
但是如果想要在代码中使用,可以这么做:private @ColorInt int getColorBackground() { int[] attrs = new int[] {android.R.attr.colorBackground}; TypedArray typedArray = getActivity().obtainStyledAttributes(attrs); int color = typedArray.getColor(0, 0xfffafafa); typedArray.recycle(); return color; }
2.2 方式2:resolveAttribute(返回TypedValue)
也有另外一种方式.public static int getColorBackground(){ TypedValue typedValue = new TypedValue(); // 第三个参数true,实际上我看不太懂是什么作用。 // 猜测:传入的id可能是一个引用,是否要继续解析该引用。 // 文档中这么描述: /* If true, resource references will be walked; if false, <var>outValue</var> may be a TYPE_REFERENCE. In either case, it will never be a TYPE_ATTRIBUTE. */ context.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true); return typedValue.data; }
2.3 两种方式的区别
方式1无法直接获取到某个Style中的值,而且它所需要的第三个参数看不太懂,获取到的TypedValue的使用方式也看不太懂。所以我更倾向于使用obtainStyledAttributes方法,比较直观。并且在自定义View中也经常使用。
2.4 两种方式结合来获取某个Style中的值
在参考文章Android,如何在代码中获取attr属性的值中,演示了如何获取某个Style中的值。
例如获取TextAppearance.Large中的textSize。
<style name="TextAppearance.Large"> <item name="android:textSize">22sp</item> <item name="android:textStyle">normal</item> <item name="android:textColor">?textColorPrimary</item> </style>
获取方式如下,因为方式1无法直接获取到某个Style中的值,所以两种方式结合:
// 先使用方式1获取到textAppearanceLarge style的resId TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true); // 然后obtain这个Id int[] attribute = new int[] { android.R.attr.textSize }; TypedArray array = getTheme().obtainStyledAttributes(typedValue.resourceId, attribute); int textSize = array.getDimensionPixelSize(0, -1); array.recycle();
2.5 obtainStyledAttributes方式获取某个Style中的值
而实际上单纯使用obtainStyledAttributes方法即可获取更加便利,并且直观:int[] attr = new int[]{android.R.attr.textSize}; TypedArray array = getTheme().obtainStyledAttributes(android.R.style.TextAppearance_Large, attr); int textSize = array.getDimensionPixelSize(0 , -1 ); array.recycle();
3. 总结
综上所述,我推荐使用obtainStyledAttributes方法!相关文章推荐
- 使用Java代码在应用层获取Android系统属性
- Android之自定义属性简单使用-----获取自定义属性
- Android使用主题属性引发的问题
- Android开发之使用ExifInterface获取拍照后的图片属性
- android中 使用 adb shel 获取/设置属性
- android 系统使用c语言获取系统属性
- Android 属性自定义及使用获取浅析
- Android高手进阶教程(十五)之---通过Location获取Address的使用!
- Android高手进阶教程(十五)之---通过Location获取Address的使用!
- 使用Git和Repo获取Android源码
- 说说Android 两种为自定义组件添加属性的使用方法和区别 推荐
- android两种使用照相功能获取图片的方法
- 使用java反射机制获取javabean中的私有属性
- Android使用Bitmap类获取图片像素的问题
- Android 获取存储卡路径和空间使用情况
- (转)使用Android标签属性style
- android开发基础[源码获取及编译、模拟器使用]
- Android高手进阶教程(十五)之---通过Location获取Address的使用!
- C#遍历系统所安装的打印机,使用WMI方式获取打印机的所有属性
- Delphi SelectSingleNode的使用 根据节点属性获取该节点