![](/img/trans.png)
[英]Xamarin.Android: Change ProgessDialog Background Color Programmatically
[英]How to change Background Color of Default Display alert in xamarin.android?
我想更改顯示警報的默認背景顏色,我在不同的網站上嘗試了很多問題 誰能幫助我?
您可以使用Rg.Plugins.Popup來模擬默認顯示警報來實現此行為,這使您可以更靈活地選擇它的外觀。
首先閱讀入門指南,在 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>
這將作為一個普通頁面,您可以將手勢識別器添加到標簽,綁定顏色使背景顏色變得生動,這完全取決於您。
默認警報:
最終結果:
如果你不想使用Rg.Plugins.Popup
,在安卓中,你可以通過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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.