如何以Xamarin形式移动右侧搜索栏的搜索图标。我正在寻找Android和iOS。对于Android,我将searchBarrenderer与以下代码无效有人知道该怎么做吗?请帮助。
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
Control.LayoutParameters=(new ActionBar.LayoutParams(GravityFlags.Right));
}
在Android中,使用自定义渲染器,您可以使用以下代码将搜索图标放置在搜索栏的右侧:
protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var searchView = base.Control as SearchView;
//Get the Id for your search icon
int searchIconId = Context.Resources.GetIdentifier("android:id/search_mag_icon", null, null);
ImageView searchViewIcon = (ImageView)searchView.FindViewById<ImageView>(searchIconId);
ViewGroup linearLayoutSearchView = (ViewGroup)searchViewIcon.Parent;
searchViewIcon.SetAdjustViewBounds(true);
//Remove the search icon from the view group and add it once again to place it at the end of the view group elements
linearLayoutSearchView.RemoveView(searchViewIcon);
linearLayoutSearchView.AddView(searchViewIcon);
}
}
在上述实现中,我只是从搜索栏视图组中删除了搜索图标,然后再次将其添加到同一视图组中。这将通常的第一个孩子放到了最后一个孩子,从而将搜索图标放在最后。