在 Xamarin Forms 平台上点击 android 按钮时如何删除动画/阴影效果

http://img4.mukewang.com/6175257f0001c19201640040.jpg

我需要删除从 xamarin 表单中点击我的 android 按钮时出现的阴影。(如上图所示。当 yes 和 no 原本是相同颜色时。按下 no 时,会在其上投射阴影,使其颜色变暗)


我按照如何删除 Xamarin Forms 上按钮的阴影中所述的示例进行操作, 但由于我的按钮颜色应该是红色,因此我没有将 PCL 中的背景颜色更改为红色。但是当点击时,阴影仍然出现在 android 按钮上。


然后,我点击了这个链接:https : //forums.xamarin.com/discussion/122752/removing-shadow-from-android-buttons 但我按钮上的阴影仍然存在。我认为就像链接中所说的那样,因为我的 xamarin 表单版本是 2.5.0.280555,所以他们提供的解决方案将不起作用。


如果我仍然需要将我的 xamarin 表单版本保持为 2.5.0.280555,那么在点击我的 Xamarin.Forms 项目时需要删除 android 按钮阴影时,是否有人对我可能做的事情有任何建议或解决方案?


它在这里说该问题已在以后的版本中解决:https : //github.com/xamarin/Xamarin.Forms/issues/1954


所以我升级到 2.5.1.52 以获取 xamarin 表单。但是在android平台上点击按钮时,我仍然看到按钮上的阴影。


我的代码更改如下:在 Style.xml


<resources>

    <style name="MyTheme" parent="MyTheme.Base">    

    </style>

    <!-- Base theme applied no matter what API -->

    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="android:buttonStyle">@style/NoShadowButton</item>


    </style>

<style name="NoShadowButton" parent="android:style/Widget.Button">

        <item name="android:stateListAnimator">@null</item>

    </style>

</resources>

我什至实现了一个自定义渲染器


[assembly: ExportRenderer(typeof(FlatButton), typeof(UnanimatedButtonRenderer))]

namespace RideNow.Droid

{

  public class UnanimatedButtonRenderer : ButtonRenderer

    {


        protected override void OnDraw(Android.Graphics.Canvas canvas)

        {

            base.OnDraw(canvas);

        }


        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)

        {

            base.OnElementChanged(e);


            if (this.Control != null && this.Element != null)

            {

                var nativeButton = (Android.Widget.Button)Control;

                nativeButton.SetShadowLayer(0, 0, 0, Android.Graphics.Color.Transparent);

                    }

                }

            }

        }

    }

}

其中 Flatbutton 只是一个自定义按钮,继承自 xamarin 表单的按钮,没有任何附加组件。但似乎没有任何效果


慕码人8056858
浏览 147回答 1
1回答

慕尼黑5688855

在您的自定义渲染器中尝试以下操作:&nbsp; &nbsp; protected override void OnElementChanged(ElementChangedEventArgs<Button> e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnElementChanged(e);&nbsp; &nbsp; &nbsp; &nbsp; if (this.Control != null && e.NewElement != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Control.StateListAnimator = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Control.Elevation = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这是我在 Android 6 模拟器上看到的:如果您希望单击时绝对没有颜色变化,您可以进一步自定义渲染器:public class UnanimatedButtonRenderer : ButtonRenderer{&nbsp; &nbsp; private FlatButton TypedElement => this.Element as FlatButton;&nbsp; &nbsp; public UnanimatedButtonRenderer(Context context) : base(context)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnElementChanged(ElementChangedEventArgs<Button> e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnElementChanged(e);&nbsp; &nbsp; &nbsp; &nbsp; if (this.Control != null && e.NewElement != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.UpdateBackground();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Control.StateListAnimator = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Control.Elevation = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnElementPropertyChanged(sender, e);&nbsp; &nbsp; &nbsp; &nbsp; if (e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.PropertyName.Equals(Button.CornerRadiusProperty.PropertyName) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.UpdateBackground();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private void UpdateBackground()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (this.TypedElement != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var background = new GradientDrawable())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background.SetColor(this.TypedElement.BackgroundColor.ToAndroid());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background.SetStroke((int)Context.ToPixels(this.TypedElement.BorderWidth), this.TypedElement.BorderColor.ToAndroid());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background.SetCornerRadius(Context.ToPixels(this.TypedElement.CornerRadius));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // customize the button states as necessary&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var backgroundStates = new StateListDrawable())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; backgroundStates.AddState(new int[] { }, background);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Control.SetBackground(backgroundStates);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在上面的代码中,背景是为所有状态设置的,但可以自定义。例如,// set a background for the un-pressed statebackgroundStates.AddState(new int[] { -Android.Resource.Attribute.StatePressed }, background);// set a background for the pressed statebackgroundStates.AddState(new int[] { Android.Resource.Attribute.StatePressed }, backgroundPressed);
打开App,查看更多内容
随时随地看视频慕课网APP