Android 如何以编程方式将 selectableItemBackground 添​​加到 ImageButton?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20531516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:59:04  来源:igfitidea点击:

How do I add selectableItemBackground to an ImageButton programmatically?

androidattrr.java-file

提问by abc32112

android.R.attr.selectableItemBackground exists, but how do I add it programatically to an ImageButton?

android.R.attr.selectableItemBackground 存在,但如何以编程方式将其添加到 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.

另外,我将如何在文档中找到答案?这里提到它,但我没有看到有关它实际使用方式的任何解释。实际上,我似乎很少发现文档有用,但我希望这是我的错,而不是文档的错。

回答by Timu?in

Here is an example using answer here: How to get the attr reference in code?

这是一个使用答案的示例: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);

回答by Andrey T

If you are using AppCompat you could use following code:

如果您使用的是 AppCompat,则可以使用以下代码:

int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
view.setBackgroundResource(backgroundResource);
typedArray.recycle();

回答by maohieng

This works for me with my TextView:

这对我有用TextView

// Get selectable background
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true);

clickableTextView.setClickable(true);
clickableTextView.setBackgroundResource(typedValue.resourceId);

Because I use AppCompat library, I use R.attr.selectableItemBackgroundnot android.R.attr.selectableItemBackground.

因为我使用 AppCompat 库,所以我R.attr.selectableItemBackground不使用android.R.attr.selectableItemBackground.

I think typedValue.resourceIdholds all drawables from selectableItemBackgroundthan using TypeArray#getResourceId(index, defValue)or TypeArray#getDrawable(index)which are retrieve only a drawable at the given index.

我认为typedValue.resourceId所有的可绘制对象都来自selectableItemBackground使用TypeArray#getResourceId(index, defValue)TypeArray#getDrawable(index)仅在给定的index.

回答by Sergio Serra

Try this method:

试试这个方法:

public Drawable getDrawableFromAttrRes(int attrRes, Context context) {
    TypedArray a = context.obtainStyledAttributes(new int[] {attrRes});
    try {
        return a.getDrawable(0);
    } finally {
        a.recycle();
    }
}

// Then just call it like this:

// 然后就这样调用它:

getDrawableFromAttrRes(R.attr.selectableItemBackground, context)

// Example
ViewCompat.setBackground(view,getDrawableFromAttrRes(R.attr.selectableItemBackground, context))