Xamarin.Android 如何在任何视图上编程设置涟漪效果?

3

这是我第一次在这里提问,所以让我们看看......

我在将涟漪效果编程设置到CardView上遇到了问题。(但我希望找到一种适用于任何类型的视图的方法) 问题在于,我的卡片是按照以下方式编程创建的:

...
        //make cardview
        CardView result = new CardView(Activity);
        //set layout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 100, 1f);
        layoutParams.SetMargins(10, 10, 10, 10);
        result.LayoutParameters = layoutParams;
        result.Tag = itemId.ToString();

        //FAILED ATTEMPT 1: 
        //result.Foreground = "?android:attr/selectableItemBackground";

        //FAILED ATTEMPT 2 : 
        //result.SetBackgroundDrawable(view.Resources.GetDrawable(Resource.Drawable.ripple));

...

现在,您可以看到我尝试使用前景属性进行操作,这是基于类似问题的答案,可以在这里找到。
第二次尝试让我感觉它走在了正确的道路上,但它使得所有的卡片都变得有点隐形:链接。(我将ripple.xml添加到了我的项目的drawable文件夹中)
我还发现了RippleDrawable类,但我真的不知道如何正确使用它。它要求使用蒙版和内容可绘制对象,但我不知道该放什么。到目前为止,我的实现如下:
result.Background = new RippleDrawable(view.Resources.GetColor(Resource.Color.green),????,?????);

我希望使用涟漪效果的主要原因是,我展示了一组卡片,并且它们都有一个onLongClick事件,用于打开弹出菜单。我想表明这些卡片是可点击的。
无论如何,我希望有人能帮助我找到解决方案。
**更新:** 在Android 5之前的代码中,卡片会变得不可见。
 ...
 result.Tag = itemId.ToString();
 TypedValue outValue = new TypedValue();
        this.Activity.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);
        result.SetBackgroundResource(outValue.ResourceId);
1个回答

2

好的,聪明的做法应该是这样的:

注意:以下代码不适用于运行在API-21或Android棒棒糖以下版本的设备。

将以下XML添加到您的布局文件夹中。

Ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/yourViewRippleColor"
tools:targetApi="lollipop">

<item>
<color android:color="@color/yourViewBackgroundColor" />
</item>

<item android:id="@android:id/mask">
  <shape android:shape="rectangle">
  <solid android:color="@color/yourViewRippleColor" />
  </shape>
</item>
</ripple>

需要时可以像这样使用:

_yourView.SetBackgroundResource(Resource.Layout.Ripple);

如果我没记错的话,你可以使用像这样的纯编程方式来完成:

注意:这种方法适用于任何版本高于蜂巢版的设备。

TypedValue outValue = new TypedValue();
        this.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);//In an Activity
        this.Activity.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);//In an Fragment
        _YourView.SetBackgroundResource(outValue.ResourceId);

祝你好运!

如果您有疑问,请回复。


我希望你已经读过了,它在 Android 版本 5 以下无法工作。 - FreakyAli
是的,我已经阅读了它,并刚刚尝试了代码,但出于某种原因,它似乎没有显示任何内容(就像我之前可见的卡片现在完全透明了)。所以我想我必须将typedValue转换为ripple xml,或者我错过了什么。它被放置在一个片段中,有关代码,请参见帖子上的编辑(如果您收到了我编辑此评论的垃圾邮件,请提前谅解)。 - DramaticlyForgottenSemiColon
尝试移除 Android.Resource 并使用您的项目资源,然后检查是否有效! - FreakyAli
刚刚尝试了这个代码:this.Activity.Theme.ResolveAttribute(Resource.Attribute.selectableItemBackground, outValue, true); 但结果仍然一样。是不是outValue获取的不对?当我使用观察器查看时,它获取了一个"{TypedValue{t=0x3/d=0x31f "res/drawable/item_background_material.xml" a=1 r=0x108045e}}"。我以为可能是我的主题有问题,但实际上里面并没有太多东西。我在想,我使用了appcompat库,这会导致问题吗? - DramaticlyForgottenSemiColon
不,那不是问题,但可能有其他原因我不知道,所以在我自己调试并查看监视器之前,我无法确定问题的原因。 - FreakyAli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接