[英]Xamarin.Android How to set the ripple effect programmatically on any view?
First time asking a question here so lets see... 第一次在这里问一个问题让我们看看......
I'm having trouble with setting the ripple effect programmatically onto a CardView. 我无法以编程方式将涟漪效果设置到CardView上。 (But i hope to find a way that works basically on any kind of view) The thing is, my cards are made programmatically like this : (但我希望找到一种基本上适用于任何视图的方式)事情是,我的卡是以编程方式制作的:
...
//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));
...
Now as you can see i tried it with the foreground property based on the answer to a similar question that can be found here . 现在,你可以看到我尝试使用前景属性,基于类似问题的答案,可以在这里找到。
the second attempt makes me feel like its on the right path but it makes all my cards invisible-ish : link . 第二次尝试让我觉得它在正确的道路上,但它使我的所有卡片看不见 - ish: link 。 (I added the ripple.xml to the drawable folder of my project) (我将ripple.xml添加到我项目的drawable文件夹中)
I also found the RippleDrawable class but i really don't understand how to use it correctly. 我还找到了RippleDrawable类,但我真的不明白如何正确使用它。 It asks for using a mask and a content drawable but i have no idea what to put there. 它要求使用面具和内容可绘制,但我不知道该放什么。 My implementation of that so far : 到目前为止我的实现:
result.Background = new RippleDrawable(view.Resources.GetColor(Resource.Color.green),????,?????);
The main reason i want the ripple effect is because i show a list of cards, and they all have a onLongClick event that opens a popupmenu. 我想要涟漪效果的主要原因是因为我显示了一个卡片列表,并且它们都有一个打开弹出菜单的onLongClick事件。 I want to indicate that the cards are clickable. 我想表明这些卡是可点击的。
Anyways, I hope somebody can help me find a solution. 无论如何,我希望有人可以帮我找到解决方案。
**UPDATE : ** cards turn invisible with code of pre-android 5. **更新:**卡片使用pre-android 5的代码转为隐形。
...
result.Tag = itemId.ToString();
TypedValue outValue = new TypedValue();
this.Activity.Theme.ResolveAttribute(Android.Resource.Attribute.SelectableItemBackground, outValue, true);
result.SetBackgroundResource(outValue.ResourceId);
Well, the smart way of doing it would be something like this : 嗯,这样做的聪明方法是这样的:
Note: The following code does not work in devices running below API-21 or Android Lollipop. 注意:以下代码在API-21或Android Lollipop下运行的设备中不起作用。
Add the following XML to your layout folder. 将以下XML添加到布局文件夹中。
Ripple.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>
And use it like this when needed: 并在需要时使用它:
_yourView.SetBackgroundResource(Resource.Layout.Ripple);
And if I am not wrong you can do it purely programmatically using something like this : 如果我没有错,你可以使用以下内容以编程方式完成它:
Note: Should work on any device above and on honeycomb. 注意:应该在蜂窝上方和上方的任何设备上工作。
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);
Goodluck! 祝好运!
In case you have queries revert. 如果您有查询还原。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.