45

我想将背景设置android.R.attr.selectableItemBackgroundLinearLayout. 使用 XML 时没有问题(有效)

<LinearLayout
    android:id="@+id/llMiner"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true" >

...但我必须在 java 代码中执行此操作,所以我尝试过

llMiner.setClickable(true);
llMiner.setBackgroundResource(android.R.attr.selectableItemBackground);

...而且它不起作用,事实上我NotFoundException在第二行得到了一个。因此,在我尝试了这个变体之后,认为资源是一种颜色。

llMiner.setClickable(true);
llMiner.setBackgroundColor(android.R.attr.selectableItemBackground);

这个不会启动异常,但是......不起作用(按下时没有改变背景,但按下时状态会发生变化)......有什么建议吗?

4

2 回答 2

110

你可以使用这种方式。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // If we're running on Honeycomb or newer, then we can use the Theme's
    // selectableItemBackground to ensure that the View has a pressed state
    TypedValue outValue = new TypedValue();
    this.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
}
于 2015-01-22T11:17:28.190 回答
0

使用这个有用的扩展

fun Context.makeCircleRippleDrawable(
    @ColorInt rippleColor: Int = ContextCompat.getColor(this, R.color.black_alpha_25),
    @ColorInt backgroundColor: Int = ContextCompat.getColor(this, android.R.color.transparent),
    @ColorInt disabledColor: Int = backgroundColor,
    elevation: Float = 0F
): Drawable {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        val content: GradientDrawable?
        val mask: GradientDrawable?

        if (backgroundColor == Color.TRANSPARENT) {
            content = null
            mask = GradientDrawable()
            mask.setColor(rippleColor)
            mask.shape = GradientDrawable.OVAL
        } else {
            content = GradientDrawable().also {
                it.shape = GradientDrawable.OVAL
                it.color = ColorStateList(
                    arrayOf(
                        intArrayOf(android.R.attr.state_activated),
                        intArrayOf(android.R.attr.state_enabled),
                        intArrayOf(-android.R.attr.state_enabled)
                    ),
                    intArrayOf(
                        backgroundColor,
                        backgroundColor,
                        disabledColor
                    )
                )
            }
            mask = null
        }

        RippleDrawable(
            ColorStateList(
                arrayOf(
                    intArrayOf(android.R.attr.state_pressed),
                    intArrayOf(android.R.attr.state_focused),
                    intArrayOf(android.R.attr.state_activated)
                ),
                intArrayOf(
                    rippleColor,
                    rippleColor,
                    rippleColor
                )
            ),
            content,
            mask
        )
    } else {

        val shapePressed = GradientDrawable()
        shapePressed.shape = GradientDrawable.OVAL
        shapePressed.setColor(rippleColor)

        val shapeDefault = GradientDrawable().also {
            it.shape = GradientDrawable.OVAL
            it.color = ColorStateList(
                arrayOf(
                    intArrayOf(android.R.attr.state_activated),
                    intArrayOf(android.R.attr.state_enabled),
                    intArrayOf(-android.R.attr.state_enabled)
                ),
                intArrayOf(
                    backgroundColor,
                    backgroundColor,
                    disabledColor
                )
            )
        }

        val stateListDrawable = StateListDrawable()
        stateListDrawable.addState(
            intArrayOf(
                android.R.attr.state_pressed,
                android.R.attr.state_enabled
            ), shapePressed
        )
        stateListDrawable.addState(intArrayOf(), shapeDefault)
        stateListDrawable
    }
}
于 2022-01-17T11:34:56.583 回答