如何在xamarin表单中更改搜索栏取消按钮图像

前端之家收集整理的这篇文章主要介绍了如何在xamarin表单中更改搜索栏取消按钮图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用自定义渲染器来更改搜索栏下划线颜色.但我不知道如何将取消按钮交叉符号(X)更改为图像,如附带的屏幕截图所示.我的自定义渲染器如下,
public class CustomSearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                LinearLayout linearLayout = this.Control.GetChildAt(0) as LinearLayout;
                linearLayout = linearLayout.GetChildAt(2) as LinearLayout;
                linearLayout = linearLayout.GetChildAt(1) as LinearLayout;

                GradientDrawable gd = new GradientDrawable();
                gd.SetStroke(0,Android.Graphics.Color.LightGray);

                linearLayout.Background = gd;                 

                AutoCompleteTextView textView = linearLayout.GetChildAt(0) as AutoCompleteTextView;
            }
        }

    }

解决方法

How to change searchbar cancel button image in xamarin forms

像这样修改你的代码

public class MySearchBarRenderer : SearchBarRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.SearchBar> 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.

原文链接:https://www.f2er.com/html/231678.html

猜你在找的HTML相关文章