我使用自定义渲染器来更改搜索栏下划线颜色.但我不知道如何将取消按钮交叉符号(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); } } }