【问题标题】:Xamarin.Android How to set the ripple effect programmatically on any view?Xamarin.Android 如何在任何视图上以编程方式设置波纹效果?
【发布时间】:2018-05-04 11:57:36
【问题描述】:

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

我无法以编程方式在 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));

...

现在如您所见,我根据here 中类似问题的答案尝试使用前台属性。

第二次尝试让我觉得它走在了正确的道路上,但它让我所有的卡片都看不见了:link。 (我将ripple.xml添加到我项目的drawable文件夹中)

我还找到了 RippleDrawable 类,但我真的不明白如何正确使用它。它要求使用蒙版和可绘制的内容,但我不知道该放什么。到目前为止我的实现:

result.Background = new RippleDrawable(view.Resources.GetColor(Resource.Color.green),????,?????);

我想要涟漪效果的主要原因是因为我显示了一个卡片列表,它们都有一个打开弹出菜单的 onLongClick 事件。我想表明这些卡片是可点击的。

无论如何,我希望有人可以帮助我找到解决方案。

**更新:**卡在 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);

【问题讨论】:

    标签: c# android xamarin xamarin.android material-design


    【解决方案1】:

    嗯,聪明的做法是这样的:

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

    将以下 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 以下不起作用
    • 是的,我读过它,刚刚尝试了代码,由于某种原因,它似乎没有显示任何内容(就像我以前可见的卡片现在变得完全透明了。所以我假设我必须打开 typedValue进入涟漪 xml 还是我错过了它放置在片段中的东西,代码请参阅帖子上的编辑(如果您收到任何垃圾邮件让我编辑此评论,我很抱歉)
    • 尝试删除 Android.Resource 并改用您的项目资源并检查是否可行!
    • 刚试过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 用于某些元素;这会导致问题吗?
    • 不,这不是问题,但可能还有其他我不知道的原因,所以在我自己调试并查看手表之前,我无法判断是什么导致了问题
    猜你喜欢
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    相关资源
    最近更新 更多