如何更改 xamarin 表单中的搜索栏取消按钮图像 [英] How to change searchbar cancel button image in xamarin forms

查看:32
本文介绍了如何更改 xamarin 表单中的搜索栏取消按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义渲染器来更改搜索栏下划线颜色.但我不知道如何将取消按钮十字符号(X)更改为图像,如所附屏幕截图所示.我的自定义渲染器如下,

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 screenshot. My custom renderer is as below,

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

    }

推荐答案

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

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

效果.

这篇关于如何更改 xamarin 表单中的搜索栏取消按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆