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 關注
- 165 瀏覽
添加回答
舉報