【问题标题】:How to remove the animation/shadow effect when an android button is tapped on Xamarin Forms Platform如何在 Xamarin Forms Platform 上点击 android 按钮时删除动画/阴影效果
【发布时间】:2018-07-24 02:22:36
【问题描述】:

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

我遵循了中所述的示例 How to remove the shadow of a button on 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

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

我的代码更改如下: 在 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);

                nativeButton.Elevation = 0;
                if (this.CheckIsCustomFont())
                {
                    var typeFace = this.GetTypeFace();
                    var typeFaceStyle = this.GetTypeFaceStyle();
                    if (typeFace != null)
                    {
                        nativeButton.SetTypeface(typeFace, typeFaceStyle);
                    }
                }
            }
        }
    }
}

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

【问题讨论】:

    标签: c# android xamarin.forms


    【解决方案1】:

    在您的自定义渲染器中尝试以下操作:

        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
    
            if (this.Control != null && e.NewElement != null)
            {
                if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)
                {
                    this.Control.StateListAnimator = null;
                }
                else
                {
                    this.Control.Elevation = 0;
                }
            }
        }
    

    这是我在 Android 6 模拟器上看到的内容:


    如果您希望在点击时绝对不改变颜色,您可以进一步自定义渲染器:

    public class UnanimatedButtonRenderer : ButtonRenderer
    {
        private FlatButton TypedElement => this.Element as FlatButton;
    
        public UnanimatedButtonRenderer(Context context) : base(context)
        {
        }
    
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
    
            if (this.Control != null && e.NewElement != null)
            {
                this.UpdateBackground();
    
                if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)
                {
                    this.Control.StateListAnimator = null;
                }
                else
                {
                    this.Control.Elevation = 0;
                }
            }
        }
    
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
            if (e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName) ||
                e.PropertyName.Equals(Button.CornerRadiusProperty.PropertyName) ||
                e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName))
            {
                this.UpdateBackground();
            }
        }
    
        private void UpdateBackground()
        {
            if (this.TypedElement != null)
            {
                using (var background = new GradientDrawable())
                {
                    background.SetColor(this.TypedElement.BackgroundColor.ToAndroid());
                    background.SetStroke((int)Context.ToPixels(this.TypedElement.BorderWidth), this.TypedElement.BorderColor.ToAndroid());
                    background.SetCornerRadius(Context.ToPixels(this.TypedElement.CornerRadius));
    
                    // customize the button states as necessary
                    using (var backgroundStates = new StateListDrawable())
                    {
                        backgroundStates.AddState(new int[] { }, background);
    
                        this.Control.SetBackground(backgroundStates);
                    }
                }
            }
        }
    }
    

    在上面的代码中,为所有状态设置了背景,但可以自定义。例如,

    // set a background for the un-pressed state
    backgroundStates.AddState(new int[] { -Android.Resource.Attribute.StatePressed }, background);
    
    // set a background for the pressed state
    backgroundStates.AddState(new int[] { Android.Resource.Attribute.StatePressed }, backgroundPressed);
    

    【讨论】:

    • 试过了,但似乎没有用。 :(当我按住android按钮时,仍然会出现暗影效果
    • 非常感谢它现在的工作。但是您可以解释这部分代码在做什么吗?是重置后台状态还是什么?使用 (var backgroundStates = new StateListDrawable()) { backgroundStates.AddState(new int[] { }, background); this.Control.SetBackground(backgroundStates); }
    猜你喜欢
    • 2016-11-23
    • 2015-03-08
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多