如何在Android上编辑SearchBar Xamarin Forms的图标?

时间:2016-04-19 16:27:06

标签: android xamarin icons edit searchbar

我需要你的帮助!

我从事跨平台的移动开发项目。我使用Visual Studio与Xamarin Forms。 我想用SearchBar(Xamarin.Forms类)创建一个页面,但是,我的问题是SearchBar图标的个性化,特别是在Android平台上。我的搜索让我一无所获。 如果您知道编辑图标的方法,我将非常感激。 (我有一个可以遵循的模型,所以我想要改变的图标,而不是颜色)。

提前谢谢!

enter image description here

4 个答案:

答案 0 :(得分:8)

enter image description here

Introduction to Custom Renderers

您可以非常轻松地对库存Xamarin.Forms控件进行子类化,以便对控件进行面向平台的小修改。

在上面的示例中,我创建了两个SearchBar子类,一个用于允许自定义图标,另一个用于删除图标,Xamarin.Android呈现如下:

enter image description here

protected override void OnElementChanged (ElementChangedEventArgs<SearchBar> e)
{
    base.OnElementChanged (e);
    if (Control != null) {
        var searchView = Control;
        searchView.Iconified = true;
        searchView.SetIconifiedByDefault (false);
        // (Resource.Id.search_mag_icon); is wrong / Xammie bug
        int searchIconId = Context.Resources.GetIdentifier ("android:id/search_mag_icon", null, null);
        var icon = searchView.FindViewById (searchIconId);
        (icon as ImageView).SetImageResource (Resource.Drawable.icon);
    }

enter image description here

protected override void OnElementChanged (ElementChangedEventArgs<SearchBar> e)
{
    base.OnElementChanged (e);
    if (Control != null) {
        var searchView = Control;
        searchView.Iconified = true;
        searchView.SetIconifiedByDefault (false);
        // (Resource.Id.search_mag_icon); is wrong / Xammie bug
        int searchIconId = Context.Resources.GetIdentifier ("android:id/search_mag_icon", null, null);
        var icon = searchView.FindViewById (searchIconId);
        icon.Visibility = Android.Views.ViewStates.Gone;

    }
}

答案 1 :(得分:4)

@ SushiHangover的方法工作正常,但一旦SearchView获得焦点,图标就会再次出现。 要解决这个问题,我使用LayoutParameters作为图标:

if(Control!= null){
    ...
    int searchIconId = Context.Resources.GetIdentifier ("android:id/search_mag_icon", null, null);
    var icon = (ImageView)searchView.FindViewById (searchIconId);
    icon.LayoutParameters = new LinearLayout.LayoutParams (0, 0);
    ...
}

答案 2 :(得分:1)

         protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var plateId = Resources.GetIdentifier("android:id/search_plate", null, null);
                var plate = Control.FindViewById(plateId);
                plate.SetBackgroundColor(Android.Graphics.Color.Transparent);

                SearchView searchView = (base.Control as SearchView);
                var searchIconId = searchView.Resources.GetIdentifier("android:id/search_map_icon", null, null);
                var searchIcon = searchView.FindViewById(searchIconId);
                searchView.SetIconifiedByDefault(false);
                searchIcon.RemoveFromParent();

                int textViewId = Resources.GetIdentifier("android:id/search_src_text", null, null);
                EditText textView = (Control.FindViewById(textViewId) as EditText);
                textView.TextAlignment = Android.Views.TextAlignment.ViewStart;
            }
        } 

这对我有用

答案 3 :(得分:0)

要自定义SearchBar的图标,您需要创建自定义渲染器,您将自定义SearchView android小部件:

[assembly:ExportRenderer(typeof(MySearchBar), typeof(Some.Namespance.MySearchBar_Droid) )]
namespace Some.Namespace
{
    public class MySearchBar_Droid : SearchBarRenderer
    {
        protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
        {
            base.OnElementChanged(args);    

            SearchView searchView = (base.Control as SearchView);

           //Customize SearchView here
        }
    }
}

有关详细信息,请参阅Introduction to Custom Renderers