Xamarin在Android中形成Shadow on Frame [英] Xamarin forms Shadow on Frame in Android

查看:106
本文介绍了Xamarin在Android中形成Shadow on Frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xamarin Forms中的Frame类非常有限,不能让我在Frame后面留下阴影。我使用以下代码为iOS创建了自定义渲染器:

The Frame class in Xamarin Forms is quite limited, and can't allow me to get a shadow behind the Frame. I've made a custom renderer for iOS using this code:

public class RatingInfoFrameRenderer : FrameRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);

        Layer.BorderColor = UIColor.White.CGColor;
        Layer.CornerRadius = 10;
        Layer.MasksToBounds = false;
        Layer.ShadowOffset = new CGSize(-2, 2);
        Layer.ShadowRadius = 5;
        Layer.ShadowOpacity = 0.4f;
    }
}

在Android上执行类似操作会导致我遇到问题,因为我对Android Native的了解有限。谁能告诉我要看什么,也许是一些好的代码示例?我还没有找到类似的东西。

Making a similar one on Android is causing me problems, since my knowledge on Android native is kind of limited. Could anyone tell me what to look at, perhaps some good code example? I haven't found anything that looks similar to this.

推荐答案

在Android平台上可以很容易,但是首先,您需要在Android资源的 Drawable 文件夹下创建阴影。例如:

It can be very easy in Android platform, but first of all, you need to create your shadow under Drawable folder of Android resources. For example:

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle">
      <solid android:color="#CABBBBBB" />
      <corners android:radius="2dp" />
    </shape>
  </item>

  <item
      android:left="0dp"
      android:right="0dp"
      android:top="0dp"
      android:bottom="2dp">
    <shape android:shape="rectangle">
      <solid android:color="@android:color/white" />
      <corners android:radius="2dp" />
    </shape>
  </item>
</layer-list>

将此文件命名为 shadow.xml,并将其放在 Drawable下 Android项目的文件夹,然后在您的 RatingInfoFrameRenderer 中:

Name this file as "shadow.xml" and place it under the Drawable folder of Android project, then in your RatingInfoFrameRenderer:

public class RatingInfoFrameRenderer : FrameRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            ViewGroup.SetBackgroundResource(Resource.Drawable.shadow);
        }
    }
}

更改阴影样式,您可以修改shadow.xml文件,有关此信息,请参考Google的官方文档: LayerList

To change the style of shadow, you can modify the shadow.xml file, for more information about this, you may refer to google's official document: LayerList.

这篇关于Xamarin在Android中形成Shadow on Frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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