How to change searchbar cancel button image in xamarin forms

前端 未结 2 1456
北恋
北恋 2020-12-18 15:24

I have used custom renderer to change the search bar underline color. But i don\'t know how to change the cancel button cross symbol(X) to image as shown in the attached scr

2条回答
  •  离开以前
    2020-12-18 15:49

    How to change searchbar cancel button image in xamarin forms

    Modify your code like this :

    public class MySearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
    
            if (Control != null)
            {
                var searchView = Control;
    
                int searchViewCloseButtonId = Control.Resources.GetIdentifier("android:id/search_close_btn", null, null);
                var closeIcon = searchView.FindViewById(searchViewCloseButtonId);
                (closeIcon as ImageView).SetImageResource(Resource.Drawable.cancel_icon);
            }
        }
    }
    

    Effect.

提交回复
热议问题