按下/下按钮时,如何更改按钮的背景颜色?
问题描述:
我有一个自定义按钮,当按下/单击按钮状态时,我想删除较深的颜色/阴影/背景颜色,所以它与按钮的背景颜色相匹配。所以看起来没有人按下它,但它仍然有一个点击事件。按下/下按钮时,如何更改按钮的背景颜色?
我想知道我该怎么做Xamarin.Forms Android?
我已经有一个自定义渲染设置,但我需要这样的东西
Control.SetBackgroundColor(Color, ButtonState.Down)
因此这将是在向下状态的颜色。 Idk,如果有办法为Android做到这一点?
此外,我使用C#作为我的观点。
答
通常情况下,您会为此使用StateListDrawable,您可以在一个Drawable中定义每个状态并将其指定为Button的背景。
你会这样定义一个XML绘制,采用了不同的图像的每个状态:
<?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>
那么你会分配该绘制的按钮布局:
<Button
...
android:background="@drawable/statelistdrawable" />
你可以也可以在C#中创建:
var drawable = new StateListDrawable();
drawable.AddState(new[] {Android.Resource.Attribute.StateFocused},
Resources.GetDrawable(Resource.Drawable.focused));
然后将其设置为背景:
var myButton = new Button(this);
myButton.Background = drawable;
状态现在应该反映在按钮上。
答
您是否尝试过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); }
}
}
的Droid:
[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;
}
}
}
}
哎呀,只是想出了你的意思是。还有一个问题,我的drawable可以是颜色而不是图像吗?如果我的按钮的颜色,我希望它成为当前页面的背景颜色......所以它是一种动态颜色?这是行不通的。谢谢! –
@Cheesebaron - 它看起来像你的答案是适用于Android的Xamarin,而不是Xamarin Forms。这是问题所要求的吗?如果是这样,我们需要删除Xamarin.Forms标签,当然? –
他有Android标签。因此Android的答案。他说他有一个自定义渲染器,因此他可以在自定义渲染器中使用此代码来获得所需的效果。 – Cheesebaron