亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Xamarin Forms 平臺上點擊 android 按鈕時如何刪除動畫/陰影效果

在 Xamarin Forms 平臺上點擊 android 按鈕時如何刪除動畫/陰影效果

C#
慕碼人8056858 2021-10-24 17:19:48
我需要刪除從 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 表單的按鈕,沒有任何附加組件。但似乎沒有任何效果
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

在您的自定義渲染器中嘗試以下操作:


    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 模擬器上看到的:

http://img1.sycdn.imooc.com//6175259b00015f7701800019.jpg

如果您希望單擊時絕對沒有顏色變化,您可以進一步自定義渲染器:


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



查看完整回答
反對 回復 2021-10-24
  • 1 回答
  • 0 關注
  • 165 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號