Xamarin.Android如何以编程方式在任何视图上设置波纹效果? [英] Xamarin.Android How to set the ripple effect programmatically on any view?

查看:80
本文介绍了Xamarin.Android如何以编程方式在任何视图上设置波纹效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First time asking a question here so lets see...

我无法以编程方式在CardView上设置波纹效果. (但是我希望找到一种基本上可以在任何视图上使用的方法)问题是,我的卡片是通过编程方式制作的,如下所示:

I'm having trouble with setting the ripple effect programmatically onto a 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. (I added the ripple.xml to the drawable folder of my project)

我也找到了RippleDrawable类,但是我真的不明白如何正确使用它.它要求使用遮罩和可绘制的内容,但我不知道该放在哪里.到目前为止,我的实现:

I also found the RippleDrawable class but i really don't understand how to use it correctly. 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),????,?????);

我想要波纹效果的主要原因是因为我显示了一张卡片列表,并且它们都具有一个onLongClick事件,可打开一个弹出菜单.我想指出这些卡片是可点击的.

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. I want to indicate that the cards are clickable.

无论如何,我希望有人能帮助我找到解决方案.

Anyways, I hope somebody can help me find a solution.

**更新:**卡在Android 5之前的代码中变得不可见.

**UPDATE : ** cards turn invisible with code of 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 :

注意:以下代码在运行于API-21或Android Lollipop以下的设备中不起作用.

Note: The following code does not work in devices running below API-21 or Android Lollipop.

将以下XML添加到布局文件夹中.

Add the following XML to your layout folder.

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

如果我没记错的话,您可以完全像这样使用编程方式来做到这一点:

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

祝你好运!

如果您有查询要还原.

这篇关于Xamarin.Android如何以编程方式在任何视图上设置波纹效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆