【问题标题】:How to set Ripple effect on a LinearLayout programmatically?如何以编程方式在 LinearLayout 上设置波纹效果?
【发布时间】:2012-02-02 16:43:04
【问题描述】:

我想将背景android.R.attr.selectableItemBackground 设置为LinearLayout。使用 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);

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

【问题讨论】:

  • 您是否正在更改活动的 onClick 方法中的颜色..???
  • 那么我认为你必须重写 onClick 方法才能完成你的功能。
  • 我正在尝试做同样的事情,但到目前为止没有成功。如果您找到了解决方案,请告诉我,或者如果有人知道这是否真的可以做到,请提供反馈。谢谢!
  • 对于任何寻找支持库对应的人,它是android.support.design.R.attr.selectableItemBackground
  • 我之前的评论现在似乎不起作用(或者可能从来没有起作用)。现在有效的是R.attr.selectableItemBackground

标签: android background android-linearlayout clickable


【解决方案1】:

你可以这样用。

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);
}

【讨论】:

  • 不适合我,白色,他们没有到达以保持 colorAccent 与棒棒糖前设备上的主题无关
  • 如果你想保持你的主题颜色使用 R.attr.selectableItemBackground 而不是 android.R.attr.selectableItemBackground
  • 使用 R.attr.selectableItemBackground 将使用 AppCompat 的属性(如果您包含该属性)并回退到淡入/淡出而不是默认情况下用于前 Lollipop 的波纹。请注意,R 是您的 packagename.R
  • 赞成有效的答案,它真的很有帮助,谢谢
【解决方案2】:

使用这个有用的扩展

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多