按钮重叠Android上的活动指示器

问题描述 投票:0回答:1

我有一个带Xamarin Forms的Xamarin Android应用程序。我想在一个按钮的右侧显示一个ActivityIndi​​cator。到目前为止,我有这个代码以前工作:

                <Grid>
                    <Grid.HeightRequest>
                        <OnPlatform x:TypeArguments="x:Double">
                            <On Platform="iOS" Value="39"/>
                            <On Platform="UWP" Value="35"/>
                            <On Platform="Android" Value="48"/>
                        </OnPlatform>
                    </Grid.HeightRequest>

                    <Button Text="{Binding Resources[RestoreBackupLabel]}" 
                        Command="{Binding RestoreCommand}"/>

                    <ActivityIndicator Color="DarkBlue" IsRunning="true" VerticalOptions="CenterAndExpand" HorizontalOptions="End" WidthRequest="40" Margin="0,2,0,2" IsVisible="True" />
                </Grid>

在IOS和UWP上,这仍然有效,但在Android上,按钮覆盖了活动指示器。我是否必须在这里专门为android声明订单?

xamarin.android
1个回答
0
投票

如果您在此处查看答案,则可以使用现有控件来执行您要在此处实现的操作

Xamarin Forms button with loading indicator

您需要添加此控件

 public class ButtonWithBusyIndicator : RelativeLayout
{
    public ButtonWithBusyIndicator()
    {
        Children.Add(new Button()
                     , Constraint.Constant(0), Constraint.Constant(0)
                     , Constraint.RelativeToParent((p) => { return p.Width; })
                     , Constraint.RelativeToParent((p) => { return p.Height; }));


        Children.Add(new ActivityIndicator { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, HeightRequest = 30, WidthRequest = 30 }
                     , Constraint.RelativeToParent((p) => { return p.Width / 2 - 15; })
                     , Constraint.RelativeToParent((p) => { return p.Height / 2 - 15; }));
    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(ButtonWithBusyIndicator), string.Empty, propertyChanged: OnTextChanged);
    public static readonly BindableProperty IsBusyProperty = BindableProperty.Create(nameof(IsBusy), typeof(bool), typeof(ButtonWithBusyIndicator), false, propertyChanged: OnIsBusyChanged);
    public static readonly BindableProperty ButtonBgColorProperty = BindableProperty.Create(nameof(ButtonBgColor), typeof(Color), typeof(ButtonWithBusyIndicator), Color.White, propertyChanged: OnButtonBgColorChanged);

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public bool IsBusy
    {
        get { return (bool)GetValue(IsBusyProperty); }
        set { SetValue(IsBusyProperty, value); }
    }

    public Color ButtonBgColor
    {
        get { return (Color)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    static void OnTextChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as ButtonWithBusyIndicator;

        if (control == null)
        {
            return;
        }

        SetTextBasedOnBusy(control, control.IsBusy, newValue as string);
    }

    static void OnIsBusyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as ButtonWithBusyIndicator;

        if (control == null)
        {
            return;
        }

        SetTextBasedOnBusy(control, (bool)newValue, control.Text);
    }

    static void SetTextBasedOnBusy(ButtonWithBusyIndicator control, bool isBusy, string text)
    {
        var activityIndicator = GetActivityIndicator(control);
        var button = GetButton(control);

        if (activityIndicator == null || button == null)
        {
            return;
        }

        activityIndicator.IsVisible = activityIndicator.IsRunning = isBusy;
        button.Text = isBusy ? string.Empty : control.Text;
    }

    static void OnButtonBgColorChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var control = bindable as ButtonWithBusyIndicator;

        if (control == null)
        {
            return;
        }

        var button = GetButton(control);

        if (button == null)
        {
            return;
        }

        button.BackgroundColor = (Color)newValue;
    }

    static ActivityIndicator GetActivityIndicator(ButtonWithBusyIndicator control)
    {
        return control.Children[1] as ActivityIndicator;
    }

    static Button GetButton(ButtonWithBusyIndicator control)
    {
        return control.Children[0] as Button;
    }
}

并使用这样的东西

<controls:ButtonWithBusyIndicator VerticalOptions="End" Text="xyz" IsBusy="true" ButtonBgColor="Green" HeightRequest="50" />

如果您有疑问,请随时恢复

祝好运

© www.soinside.com 2019 - 2024. All rights reserved.