SearchView-Widget 在 Xamarin.Forms (Android) 中不显示插入符号和下划线

SearchView-Widget is not displaying caret and underline in Xamarin.Forms (Android)

我尝试在 Xamarin.Forms (Android) 上的内容页面导航中放置一个搜索栏。我使用以下代码(来自 https://www.linkedin.com/pulse/xamarin-forms-contentpage-searchbar-navigation-bar-vipin-mathews):

首先我定义了我的搜索栏资源:

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
  <item android:id="@+id/action_search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

然后我使用自定义渲染器将其显示在 ContentPage 的导航页眉中:

[assembly: ExportRenderer(typeof(SearchPage), typeof(SearchPageRenderer))]

namespace Muellerchur.Xamos.Streckenkontrolle.Droid.CustomRenderer
{
    using Android.Runtime;
    using Android.Support.V7.Widget;
    using Android.Text;
    using Android.Views.InputMethods;

    using Muellerchur.Xamos.Streckenkontrolle.Droid;

    using Plugin.CurrentActivity;

    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;

    using Resource = Muellerchur.Xamos.Streckenkontrolle.Droid.Resource;

    /// <summary>
    ///     The search page renderer.
    /// </summary>
    public class SearchPageRenderer : PageRenderer
    {
        /// <summary>
        ///     Gets or sets the search view.
        /// </summary>
        private SearchView searchView;

        /// <summary>
        ///     Gets or sets the toolbar.
        /// </summary>
        private Toolbar toolbar;

        /// <summary>
        ///     Reaction on the disposing of the page.
        /// </summary>
        /// <param name="disposing">A value indicating whether disposing.</param>
       

        /// <summary>
        ///     Reaction on the element changed event.
        /// </summary>
        /// <param name="e">The event argument.</param>
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);

            if (e?.NewElement == null || e.OldElement != null)
            {
                return;
            }

            this.AddSearchToToolBar();
        }

        /// <summary>
        ///     Adds a search item to the toolbar.
        /// </summary>
        private void AddSearchToToolBar()
        {
            this.toolbar = (CrossCurrentActivity.Current?.Activity as MainActivity)?.FindViewById<Toolbar>(Resource.Id.toolbar);

            if (this.toolbar != null)
            {
                this.toolbar.Title = this.Element.Title;
                this.toolbar.InflateMenu(Resource.Menu.mainmenu);

                this.searchView = this.toolbar.Menu?.FindItem(Resource.Id.action_search)?.ActionView?.JavaCast<SearchView>();

                if (this.searchView != null)
                {
                    this.searchView.ImeOptions = (int)ImeAction.Search;
                    this.searchView.InputType = (int)InputTypes.TextVariationNormal;
                    this.searchView.MaxWidth = int.MaxValue;
                }
            }
        }  
       
    }
}

但是,如果用户输入 SearchView,则不会显示插入符号和下划线:

我想显示这样的东西(没有图标)

我怎样才能实现这个布局?还是 phone 依赖?我用的是 HUAWEI P9 Android-Phone.

SearchView-Widget is not displaying caret and underline in Xamarin.Forms (Android)

我已经重现了你的问题,我认为不是phone-dependant。您的 AddSearchToToolBar 方法出现了一些问题。

  1. 显示caret

删除this.searchView.InputType = (int)InputTypes.TextVariationNormal,插入符号将正常显示。

  1. 显示underline

感谢 vArDo 的 advice, you could set a background which contains a underline for SearchView. To create drawable selector, so that proper image is displayed based on view state, one for state when text field is selected (named textfield_search_selected_holo_light.9.png) and one for where it's not (named textfield_search_default_holo_light.9.png)。

创建包含以下内容的文件 res/drawable/texfield_searchview_holo_light.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true" android:drawable="@drawable/textfield_search_selected_holo_light" />
   <item android:drawable="@drawable/textfield_search_default_holo_light" />
</selector>

SearchView设置Drawable选择器:

this.searchView.SetBackgroundResource(Resource.Drawable.texfield_searchview_holo_light);

效果是这样的: