如何更改 xamarin.android 中默认显示警报的背景颜色?

2024-02-04

我想更改显示警报的默认背景颜色,我在不同的网站上尝试了很多问题 谁能帮我 ?


您可以使用以下方法实现此行为Rg.Plugins.Popup https://github.com/rotorgames/Rg.Plugins.Popup为了模仿默认显示警报,这使您可以更灵活地选择其外观。

首先阅读入门 https://github.com/rotorgames/Rg.Plugins.Popup/wiki/Getting-started,在 Android 上,您将在 OnCreate 中需要它:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    Rg.Plugins.Popup.Popup.Init(this, bundle); //Initialize the plugin

    Xamarin.Forms.Forms.Init(this, bundle);
    LoadApplication (new App ());
}

之后,您需要创建一个从 PopupPage 扩展的 View:

背后代码:

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AlertPage : PopupPage
{
    public AlertPage()
    {
        InitializeComponent();
    }
}

Xaml 的外观如下(我尝试尽可能模仿默认警报,您可以对其进行调整以实现您想要的外观):

<popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
             x:Class="BlankAppTest.Views.AlertPage">

    <popup:PopupPage.Animation>
        <animations:ScaleAnimation 
            PositionIn="Bottom"
            PositionOut="Bottom"
            ScaleIn="1.2"
            ScaleOut="0.8"
            DurationIn="400"
            DurationOut="300"
            EasingIn="SinOut"
            EasingOut="SinIn"
            HasBackgroundAnimation="True" />
    </popup:PopupPage.Animation>

    <StackLayout Margin="30,0,30,0" VerticalOptions="Center" BackgroundColor="#121212">
        <Grid VerticalOptions="Fill">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="50"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="50"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Text="Alert Title" TextColor="White" FontAttributes="Bold" Margin="20,20,20,0"></Label>
            <Label Grid.ColumnSpan="3" TextColor="White" Grid.Row="1" VerticalOptions="StartAndExpand" Text="This is a Custom Popup made it Rg.Plugins.Popup to mimic the Default Android Alert" Margin="20,0"></Label>


            <Label Margin="0,0,0,20" Grid.Column="2" FontAttributes="Bold" Grid.Row="2" VerticalOptions="End" Text="Yes" TextColor="White"></Label>
            <Label Margin="0,0,0,20" Grid.Column="1" FontAttributes="Bold" Grid.Row="2" VerticalOptions="End" Text="No" TextColor="White"></Label>
        </Grid>
    </StackLayout>

</popup:PopupPage>

这将作为一个普通页面,您可以将手势识别器添加到标签中,绑定颜色以使背景颜色是动态的,这一切都取决于您。

默认警报:

最终结果:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改 xamarin.android 中默认显示警报的背景颜色? 的相关文章

随机推荐