如何以Xamarin形式移动右侧搜索栏的搜索图标 [英] How to move search icon of search bar at right hand side in xamarin forms

查看:43
本文介绍了如何以Xamarin形式移动右侧搜索栏的搜索图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以xamarin形式移动右侧搜索栏的搜索图标. 我正在寻找android和iOS. 对于Android,我使用SearchBarRenderer和下面的代码不起作用 有人知道怎么做吗?请帮忙.

How to move search icon of search bar at right hand side in xamarin forms. I am looking for android and IOS. For Android I used SearchBarRenderer With below code which does not worked Anyone know how to do it?Please help.

 protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        Control.LayoutParameters=(new ActionBar.LayoutParams(GravityFlags.Right));
    }

推荐答案

在android中,使用自定义渲染器,您可以使用以下代码将搜索图标放置在搜索栏的右侧:

In android, with a custom renderer, you can use the following code to place the search icon at the right side of your search bar:

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

在上述实现中,我只是从搜索栏视图组中删除了搜索图标,然后将其再次添加到同一视图组中.这样通常会将第一个孩子放到最后一个孩子,因此将搜索图标放在最后.

In the above implementation, I simply removed the search icon from the search bar view group and then added it again to the same view group. This placed the normally first child to the last child, thus placing the search icon at the end.

这篇关于如何以Xamarin形式移动右侧搜索栏的搜索图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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