0

我想更改禁用按钮的透明度级别。

在此处输入图像描述

禁用按钮视图

在此处输入图像描述

启用按钮视图

[assembly: ExportRenderer(typeof(LabelButton), typeof(SkyRacing.Droid.LabelButtonRenderer))]
namespace SkyRacing.Droid
{
    public class LabelButtonRenderer : MaterialButtonRenderer
    {
        public LabelButtonRenderer(Context ctx) : base(ctx)
        {

        }

        protected override void OnDraw(Android.Graphics.Canvas canvas)
        {
            base.OnDraw(canvas);

            if (!Element.IsEnabled)
                Element.Opacity = 0.9;
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == nameof(Xamarin.Forms.Button.IsEnabled))
            {
                if (!Element.IsEnabled)
                    Element.Opacity = 0.9;
            }
        }
    }
}

我尝试创建一个自定义渲染器并将禁用按钮的不透明度更改为 0.9,但它仍然太透明。如何降低按钮的透明度?(仅供参考,在调试模式下,我检查了该Element属性。甚至在我设置为 0.9 之前它已经是 1,想知道它是如何太透明了)。

4

2 回答 2

3

您可以自定义任何属性,如果Button它是disabled使用Style. 更改 的值Opacity以更改按钮的透明度级别。

<ContentPage.Resources>

    <Style x:Key="BaseButton" TargetType="Button">
        <Style.Triggers>
            <Trigger TargetType="Button"
                     Property="IsEnabled"
                     Value="False">
                <Setter Property="TextColor" Value="#5019171c" />
                <Setter Property="BackgroundColor" Value="Red" />
                <Setter Property="Opacity" Value="0.9" />
            </Trigger>
        </Style.Triggers>
    </Style>

</ContentPage.Resources>

<StackLayout>
    <!-- Place new controls here -->
    <Button Text="click" Clicked="Button_Clicked"/>

    <Button Text="sampleButton"  BackgroundColor="Blue" x:Name="sampleButton" Style="{StaticResource BaseButton}"/>
</StackLayout>

更新代码:

[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(LabelButtonRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace App389.Droid
{
    public class LabelButtonRenderer : MaterialButtonRenderer
    {
        public LabelButtonRenderer(Context ctx) : base(ctx)
        {

        }


        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {

            if (e.PropertyName == nameof(Xamarin.Forms.Button.IsEnabled))
            {
                if (Element.IsEnabled == false) {
                                       
                    Control.BackgroundTintList = Android.App.Application.Context.Resources.GetColorStateList(Resource.Color.button_normalcolor);
                }
                else
                {
                    Control.BackgroundTintList = Android.App.Application.Context.Resources.GetColorStateList(Resource.Color.button_disablecolor);
                }
            }

        }       
    }
}

并在 colors.xml 中添加颜色:

<resources>
    <color name="launcher_background">#FFFFFF</color>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
  
    <color name="button_normalcolor">#ff00</color>
    <color name="button_disablecolor">#FF4081</color>

</resources>

更新 iOS 代码:

[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(LabelButtonRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
public class LabelButtonRenderer : MaterialButtonRenderer
{

    protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
    {
        base.OnElementChanged(e);

    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {

        Console.WriteLine(e.PropertyName.ToString());

        if (e.PropertyName == nameof(Xamarin.Forms.Button.IsEnabled))
        {
            Control.BackgroundColor = Element.BackgroundColor.ToUIColor();

        }

    }
}
于 2020-09-25T06:28:49.497 回答
0

@Jack 提供的解决方案对我有用。此外,您可以尝试这样:

<Button x:Name="testButton" Text="Test">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="Disabled">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                            <Setter Property="Opacity" Value="0.9"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Button>

如果这也不起作用,请分享有关您的 xaml 的更多详细信息

于 2020-09-25T10:50:59.620 回答