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

问题描述 投票:-1回答:2

我想更改显示警报的默认背景颜色,我已经在不同站点尝试了许多问题谁能帮我吗?

android xamarin.forms xamarin.android
2个回答
0
投票

您可以使用Rg.Plugins.Popup模仿默认的显示警报来实现此行为,这使您可以更灵活地查看其外观。

首先阅读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扩展的视图:

后面的代码:

[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>

这将作为普通页面,您可以将Gesture Recognizers添加到标签,绑定颜色以使背景颜色具有动态效果,这完全取决于您。

默认警报:

enter image description here

最终结果:

enter image description here


0
投票

如果您不想使用Rg.Plugins.Popup,在android中,您可以使用AlertDialog和自定义样式来实现它。可以通过DependencyService调用它。

[assembly: Dependency(typeof(DeviceOpenAlertService))]
namespace App2.Droid
{
    class DeviceOpenAlertService : IDeviceOpenAlertService
    {
        public void Open()
        {

            var alert = new AlertDialog
                 .Builder(CrossCurrentActivity.Current.Activity, Resource.Style.MyDialogStyle)
                 .SetTitle("Alert Title")
                 .SetMessage("Do you want to close this application")
                 .SetPositiveButton("Yes", new myButtonClicklistener())
                 .SetNegativeButton("No", new myButtonClicklistener())
                 .Create();

            alert.Show();
        }
    }
}

styles.xml添加以下样式

 <style name="MyDialogStyle" parent="android:Theme.Material.Light.Dialog.NoActionBar">
    <!--Dialog Background Color-->
    <item name="android:colorBackground">#FF0000</item>

  </style>

  <style name="MyButtonsStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
    <!-- text color for the button -->
    <item name="android:textColor">#00ff00</item>
  </style>

在PCL中调用。

 DependencyService.Get<IDeviceOpenAlertService>().Open();

这里正在运行GIF。

enter image description here

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