【问题标题】:Programmatically set '?selectableItemBackground' on Android view在 Android 视图上以编程方式设置“?selectableItemBackground”
【发布时间】:2016-06-23 09:35:48
【问题描述】:

在xml中,我经常这样做是为了模仿onClick效果:

<android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:foreground="?selectableItemBackground">

    ...

</android.support.v7.widget.CardView>

有没有办法在java中访问?selectableItemBackground

【问题讨论】:

    标签: android xml android-layout android-cardview


    【解决方案1】:

    对于appcompat你可以使用,

    TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
    cardView.setBackgroundResource(outValue.resourceId);
    

    【讨论】:

    • 我搜索了很多,找到了很多答案,但这肯定是最好的。非常感谢!
    • 不适合我我以编程方式制作图像视图并设置此资源以及可点击和可聚焦的 true
    • 这会改变背景而不是前景,因此如果您已经有视图的自定义背景,它将替换它。我不知道这怎么可能是正确的答案
    • 显示为紫色。有什么办法可以改变颜色?
    • 这很有帮助。
    【解决方案2】:

    对于那些使用 Kotlin 的人,这里有一些在 Android 视图类型上添加 Ripple 的扩展函数:

    private fun View.addRipple() = with(TypedValue()) {
        context.theme.resolveAttribute(android.R.attr.selectableItemBackground, this, true)
        setBackgroundResource(resourceId)
    }
    
    private fun View.addCircleRipple() = with(TypedValue()) {
        context.theme.resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, this, true)
        setBackgroundResource(resourceId)
    }
    

    【讨论】:

    • 使用 android.R.attr... 将支持 API 级别 >= 21,而不是如果使用 R.attr... 则向后兼容。
    • 当我点击cardview时它消失了!
    • addCircleRipple() 不支持 API 级别 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { view.addCircleRipple() }
    【解决方案3】:

    我一直在寻找相同的解决方案。我稍微更改了this 答案,使其更适合所提出的问题。从您的构造函数中调用以下代码。

    private void setClickableAnimation(Context context)
    {
        TypedValue outValue = new TypedValue();
        context.getTheme().resolveAttribute( 
            android.R.attr.selectableItemBackground, outValue, true);        
        setForeground(getDrawable(context, outValue.resourceId));
    }
    

    【讨论】:

      【解决方案4】:

      您应该将其引用为

      android.R.attr.selectableItemBackground

      【讨论】:

        【解决方案5】:

        对于我正在使用的 Kotlin

        binding.yourCoolView.apply {
            background = with(TypedValue()) {
                context.theme.resolveAttribute(
                    R.attr.selectableItemBackground, this, true)
                ContextCompat.getDrawable(context, resourceId)
            }
        }
        

        【讨论】:

          【解决方案6】:

          根据@Wirling 的回答,我们可以使用前景来设置颜色和波纹效果

          注意:前台需要 android API level 23(M) 及以上:

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
              TypedValue outValue = new TypedValue();
              getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
              view.setForeground(getDrawable(getContext(), outValue.resourceId));
          }
          

          【讨论】:

            【解决方案7】:

            试试下面的代码。

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

            【讨论】:

            • 很抱歉,这段代码对我来说没有意义,如果您没有从中获取任何资源,那么获取TypedArray 有什么意义?
            【解决方案8】:

            来自一个片段:

            TypedValue outValue = new TypedValue();
            requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            cardView.setBackgroundResource(outValue.resourceId);
            

            从在其构造函数中声明上下文或相同片段的适配器:

            TypedValue outValue = new TypedValue();
            fragment.requireContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            holder.cardView.setBackgroundResource(outValue.resourceId);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-07-10
              • 2015-02-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多