我尝试将搜索栏放在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没有插入符号,则不会显示下划线:
我想要显示这样的内容(没有图标)
我怎样才能实现这种布局?还是依赖手机?我正在使用 HUAWEI P9 Android手机。
答案 0 :(得分:1)
SearchView-Widget未在Xamarin.Forms(Android)中显示插入符和下划线
我已经复制了您的问题,我认为它不是phone-dependant
。您的AddSearchToToolBar
方法中存在一些问题。
caret
:删除this.searchView.InputType = (int)InputTypes.TextVariationNormal
,插入符号将正常显示。
underline
:感谢vArDo advice,您可以设置一个背景,其中包含SearchView
的下划线。要创建drawable
选择器,以便根据视图状态显示正确的图像,一个用于选择文本字段时的状态(名为textfield_search_selected_holo_light.9.png),另一个用于不显示的地方(名为{{ 3}})。
使用以下内容创建文件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>
为Drawable
设置SearchView
选择器:
this.searchView.SetBackgroundResource(Resource.Drawable.texfield_searchview_holo_light);
这样的效果: