我正在为条目创建自定义渲染器。我主要是想在输入字段的左侧添加一个图标。我在 Android 中使用 SetCompoundDrawablesRelativeWithIntrinsicBounds() 并在 iOS 中使用 LeftView 实现了这一点。
当 ClearButtonVisibility 设置为“编辑时”时会出现问题。在条目中输入内容并单击清除按钮后,左侧图标与文本一起清除(仅在Android中出现。在iOS中,仅清除文本。我意识到内置的清除按钮图像在复合drawables,所以我用我的图标和内置图标设置复合drawables。
最好的解决方法是什么?我希望这样当单击清除按钮时,只清除文本而不清除图标。
注意:图标会立即清除。当触发任何其他事件时,图标会返回。
安卓渲染器:
[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]
namespace Styles.Android.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
// update control UI
UpdateControl();
base.OnElementPropertyChanged(sender, e);
}
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
// update control UI
UpdateControl();
base.OnElementChanged(e);
}
private void UpdateControl()
{
if (Element == null || Control == null)
return;
var entry = Element as CustomEntry;
// set border/background
var gradientBackground = new GradientDrawable();
gradientBackground.SetShape(ShapeType.Rectangle);
gradientBackground.SetColor(Element.BackgroundColor.ToAndroid());
gradientBackground.SetCornerRadius(entry.CornerRadius);
gradientBackground.SetStroke(entry.BorderWidth, entry.BorderColor.ToAndroid());
Control.SetBackground(gradientBackground);
// set appropriate icon
int resID = Resources.GetIdentifier("icon_image", "drawable", this.Context.PackageName);
var drawable = ContextCompat.GetDrawable(this.Context, resID);
Control.SetCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, Control.GetCompoundDrawables()[2], null);
Control.CompoundDrawablePadding = entry.IconPadding;
paddingLeft = entry.IconPadding;
// set padding
Control.SetPadding(Control.PaddingLeft, Control.PaddingTop, Control.PaddingRight, Control.PaddingBottom);
}
}
}