我有一个自定义按钮,我想在按下/点击按钮状态时删除较暗的颜色/阴影/背景颜色,因此它已经与按钮的背景颜色匹配。所以看起来没有人按下它但它仍然附加了一个点击事件。
我想知道如何为Xamarin.Forms Android做到这一点?
我已经有自定义渲染器设置,但我需要像
这样的东西Control.SetBackgroundColor(Color, ButtonState.Down)
所以在羽绒状态下它就是那种颜色。 Idk,如果有这样的方法为Android做到这一点?
另外,我正在使用C#来查看。
答案 0 :(得分:0)
通常你会使用StateListDrawable,你可以在一个Drawable中定义每个状态,并将其指定为Button的背景。
你可以像这样定义一个XML drawable,为每个状态使用不同的图像:
console.log(format("mark")); //Mark (correct)
console.log(format("mark o'loughlan")); //Mark O'Loughlan (correct)
console.log(format("mark's audi")); //Mark'S Audi (incorect)
然后,您可以将此drawable分配给布局中的按钮:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed_img" android:state_pressed="true"/>
<item android:drawable="@drawable/hovered_img" android:state_hovered="true"/>
<item android:drawable="@drawable/focused_img" android:state_focused="true"/>
<item android:drawable="@drawable/default_img"/>
</selector>
您也可以在C#中创建它:
<Button
...
android:background="@drawable/statelistdrawable" />
然后将其设置为背景:
var drawable = new StateListDrawable();
drawable.AddState(new[] {Android.Resource.Attribute.StateFocused},
Resources.GetDrawable(Resource.Drawable.focused));
状态现在应该反映在按钮上。
答案 1 :(得分:0)
您是否尝试过Xamarin论坛链接here中的代码。我没有亲自尝试过,但看起来它可以用来做你想要的,包括使用动态颜色。也许是这样的(我从内存中写了一些代码,所以如果它不起作用,请告诉我):
形式:
public class FancyButton : Button {
/// <summary>
/// The color pressed down color property.
/// </summary>
public static readonly BindableProperty PressedDownColorProperty = BindableProperty.Create(nameof(PressedDownColor), typeof(Color), typeof(FancyButton), default(Color));
/// <summary>
/// Gets or sets the pressed down color.
/// </summary>
/// <value>The color to change to when the button is pressed down string.</value>
public Color PressedDownColor {
get { return (Color)GetValue(PressedDownProperty); }
set { SetValue(PressedDownProperty, value); }
}
}
德罗伊德:
[assembly: ExportRenderer(typeof(App2.FancyButton), typeof(FancyButtonAndroid))]
namespace App2.Droid {
public class FancyButtonAndroid : ButtonRenderer {
private Color _pressedDownColor;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e) {
base.OnElementChanged(e);
Android.Widget.Button thisButton = Control as Android.Widget.Button;
FancyButton formsButton = (FancyButton)Element;
_pressedDownColor = formsButton.PressedDownColor;
thisButton.Touch += (object sender, Android.Views.View.TouchEventArgs e2) => {
if (e2.Event.Action == MotionEventActions.Down) {
thisButton.SetBackgroundColor(_pressedDownColor.ToAndroid());
} else if (e2.Event.Action == MotionEventActions.Up) {
thisButton.SetBackgroundColor(Xamarin.Forms.Color.Default.ToAndroid());
}
};
}
/// <summary>
/// Raises the element property changed event.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">The event arguments</param>
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
base.OnElementPropertyChanged(sender, e);
FancyButton formsButton = Element as FancyButton;
if(formsButton != null && e.PropertyName == FancyButton.PlaceholderProperty.PropertyName) {
_pressedDownColor = formsButton.PressedDownColor;
}
}
}
}