Jon*_*ona 45 android android-layout
我希望通过代码从属性中获取指向引用.在我的xml布局中,我可以轻松地获得引用的drawable,如下所示:
android:background="?attr/listItemBackground"
Run Code Online (Sandbox Code Playgroud)
属性引用由我的主题设置.我想看看是否可以通过代码获得引用的drawable.
我可以通过创建样式attr并在自定义视图中读取值来解决这个问题,但在这种情况下,我想弄清楚这是否可行而不做所有这些.我认为这是可能的,但我还没有找到获得该属性参考的方法.
谢谢!
Mar*_*lts 106
这是你如何做到的:
// Create an array of the attributes we want to resolve
// using values from a theme
int[] attrs = new int[] { R.attr.listItemBackground /* index 0 */};
// Obtain the styled attributes. 'themedContext' is a context with a
// theme, typically the current Activity (i.e. 'this')
TypedArray ta = themedContext.obtainStyledAttributes(attrs);
// To get the value of the 'listItemBackground' attribute that was
// set in the theme used in 'themedContext'. The parameter is the index
// of the attribute in the 'attrs' array. The returned Drawable
// is what you are after
Drawable drawableFromTheme = ta.getDrawable(0 /* index */);
// Finally, free the resources used by TypedArray
ta.recycle();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25064 次 |
| 最近记录: |