1 回答
TA贡献1848条经验 获得超2个赞
在您的自定义渲染器中尝试以下操作:
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Control != null && e.NewElement != null)
{
if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)
{
this.Control.StateListAnimator = null;
}
else
{
this.Control.Elevation = 0;
}
}
}
这是我在 Android 6 模拟器上看到的:
如果您希望单击时绝对没有颜色变化,您可以进一步自定义渲染器:
public class UnanimatedButtonRenderer : ButtonRenderer
{
private FlatButton TypedElement => this.Element as FlatButton;
public UnanimatedButtonRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
{
base.OnElementChanged(e);
if (this.Control != null && e.NewElement != null)
{
this.UpdateBackground();
if (Build.VERSION.SdkInt > BuildVersionCodes.Lollipop)
{
this.Control.StateListAnimator = null;
}
else
{
this.Control.Elevation = 0;
}
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName.Equals(VisualElement.BackgroundColorProperty.PropertyName) ||
e.PropertyName.Equals(Button.CornerRadiusProperty.PropertyName) ||
e.PropertyName.Equals(Button.BorderWidthProperty.PropertyName))
{
this.UpdateBackground();
}
}
private void UpdateBackground()
{
if (this.TypedElement != null)
{
using (var background = new GradientDrawable())
{
background.SetColor(this.TypedElement.BackgroundColor.ToAndroid());
background.SetStroke((int)Context.ToPixels(this.TypedElement.BorderWidth), this.TypedElement.BorderColor.ToAndroid());
background.SetCornerRadius(Context.ToPixels(this.TypedElement.CornerRadius));
// customize the button states as necessary
using (var backgroundStates = new StateListDrawable())
{
backgroundStates.AddState(new int[] { }, background);
this.Control.SetBackground(backgroundStates);
}
}
}
}
}
在上面的代码中,背景是为所有状态设置的,但可以自定义。例如,
// set a background for the un-pressed state
backgroundStates.AddState(new int[] { -Android.Resource.Attribute.StatePressed }, background);
// set a background for the pressed state
backgroundStates.AddState(new int[] { Android.Resource.Attribute.StatePressed }, backgroundPressed);
- 1 回答
- 0 关注
- 145 浏览
添加回答
举报