如何在Android上编辑SearchBar Xamarin Forms的图标? [英] How to edit icon of SearchBar Xamarin Forms on Android?
问题描述
我需要你的帮助!
我在跨平台的移动开发项目上工作。我使用Visual Studio与Xamarin Forms。
我想创建一个带有SearchBar(Xamarin.Forms类)的页面,但是,我的问题是SearchBar图标的个性化,特别是在Android平台上。我的搜索让我一无所获。
如果您知道编辑图标的方法,我将非常感激。 (我有一个模型要遵循,所以我想要更改的图标,而不是颜色)。
提前谢谢!
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);是错的/ Xammie bug
int searchIconId = Context.Resources.GetIdentifier(android:id / search_mag_icon,null,null);
var icon = searchView.FindViewById(searchIconId);
(图标为ImageView).SetImageResource(Resource.Drawable.icon);
}
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);是错的/ 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;
}
}
I need your help !
I work on a mobile development project, cross-platform. I use Visual Studio with Xamarin Forms. I want to make a page with a SearchBar (Xamarin.Forms class), but, my problem is the personalisation of the SearchBar icon, especially on the Platform Android. My search led me to nothing. I would be very grateful if you know of a way to edit icon. (I have a model to follow, so It’s well the icon that I want to change and not the color).
Thank you in advance !
Introduction to Custom Renderers
You can subclass the stock Xamarin.Forms
controls fairly easily for making minor platform-oriented changes to the controls.
In the examples above, I create two SearchBar subclasses, one to allow a custom icon and the other to remove the icon, the Xamarin.Android
renders look like:
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);
}
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;
}
}
这篇关于如何在Android上编辑SearchBar Xamarin Forms的图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!