我试图在我的Xamarin.Forms项目中使用Search Widget,因为人们在Xamarin.Android中使用它但是它无效。我想要与您在下面的图片中看到的结果相同。enter image description here 这是我的代码:
**Main Activity :**
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Android.Support.V4.App;
using Android.Support.V4.Widget;
using Android.Util;
using Android.Support.V4.Content;
namespace GitRemote.Droid
{
[Activity(Label = "GitRemote", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle);
Forms.Init(this, bundle);
LoadApplication(new App());
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
Android.Widget.SearchView searchView;
MenuInflater.Inflate(Resource.Menu.menu_home, menu);
var searchItem = menu.FindItem(Resource.Id.action_search);
var provider = MenuItemCompat.GetActionView(searchItem);
searchView = provider.JavaCast<Android.Widget.SearchView>();
// searchView.SetIconifiedByDefault(false);
searchView.QueryTextSubmit += (sender, args) =>
{
Toast.MakeText(this, "You searched: " + args.Query, ToastLength.Short).Show();
};
//searchView.QueryTextSubmit += (sender, args) =>
//{
// var view = sender as Android.Support.V7.Widget.SearchView;
// if ( view != null )
// view.ClearFocus();
//};
//return base.OnCreateOptionsMenu(menu);
return true;
}
}
}
**menu_home:**
<?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="@string/search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always|withText"
app:actionViewClass="android.widget.SearchView"/>
</menu>
答案 0 :(得分:2)
我遇到了与Xamarin.Forms搜索组件相同的问题。
Xamrin.Forms有一个名为SearchBar的控件。确实......在模拟器上显示但是当您在Android设备(例如Samsung S8)上安装应用程序时,SearchBar不可见。
我可以采取一种解决方法: 手动设置搜索栏的高度。 42是SearchBar的默认值。
HeightRequest="42"
这个问题在这里得到解决 https://bugzilla.xamarin.com/show_bug.cgi?id=43975