[英]How can i change the background color or apply an color to an Item in Drop down list
[英]How can I change the background color of the button when it is pressed/down?
我有一個自定義按鈕,按下/單擊按鈕狀態時,我想刪除較深的顏色/陰影/背景色,因此它已經與按鈕的背景色匹配。 因此,似乎沒有人按下它,但它仍然附加了click事件。
我想知道如何為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論壇鏈接試過的代碼在這里 。 我沒有親自嘗試過,但是看起來它可以用來做您想要的事情,包括使用動態顏色。 也許是這樣的(我從內存中寫了一些代碼,所以讓我知道它是否不起作用):
形式:
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;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.