【问题标题】:How to move search icon of search bar at right hand side in xamarin forms如何在 xamarin 表单中移动右侧搜索栏的搜索图标
【发布时间】:2017-12-09 10:57:53
【问题描述】:

如何在 xamarin 表单中移动右侧搜索栏的搜索图标。 我正在寻找安卓和IOS。 对于 Android,我使用 SearchBarRenderer 并使用以下代码,但它不起作用 有人知道怎么做吗?请帮忙。

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

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多