本文介绍了我如何添加selectableItemBackground到的ImageButton编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
android.R.attr.selectableItemBackground存在,但我怎么编程把它添加到一个ImageButton的?
android.R.attr.selectableItemBackground exists, but how do I add it programatically to an ImageButton?
另外,我怎么会去寻找答案的文档中?它提到这里,但我没有看到它是如何实际使用任何解释。其实,我似乎很少发现的文件是有用的,但我希望这是我的错,而不是该文档。
Also, how would I go about finding the answer in the documentation? It's mentioned here, but I don't see any explanation of how it's actually used. Actually, I rarely seem to find the documentation useful, but I'm hoping that's my fault and not that of the documentation.
推荐答案
下面是一个例子使用的答案在这里:的
Here is an example using answer here: How to get the attr reference in code?
// Create an array of the attributes we want to resolve
// using values from a theme
// android.R.attr.selectableItemBackground requires API LEVEL 11
int[] attrs = new int[] { android.R.attr.selectableItemBackground /* index 0 */};
// Obtain the styled attributes. 'themedContext' is a context with a
// theme, typically the current Activity (i.e. 'this')
TypedArray ta = obtainStyledAttributes(attrs);
// Now 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 resources used by TypedArray
ta.recycle();
// setBackground(Drawable) requires API LEVEL 16,
// otherwise you have to use deprecated setBackgroundDrawable(Drawable) method.
imageButton.setBackground(drawableFromTheme);
// imageButton.setBackgroundDrawable(drawableFromTheme);
这篇关于我如何添加selectableItemBackground到的ImageButton编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!