[英]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.