AlertDialog按钮在Xamarin Droid项目中以固定宽度堆叠

时间:2017-11-06 14:34:42

标签: android xamarin.forms alertdialog

出于某种原因,当我将“android:minWidth”行添加到styles.xml时,它将按钮堆叠在一起。没有线,按钮看起来很好。我只是想修复警报对话框的宽度并保持其他所有内容相同。这是我的styles.xml文件的基础知识:

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:alertDialogTheme">@style/MyAlertDialogStyle</item>
</style>

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#2E82F7</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:minWidth">700dp</item>
    <!--<item name="android:textSize">36dp</item>-->
    <!--<item name="android:layout_width">match_parent</item>-->
</style>

以下是我的提醒with the fixed widthwithout the fixed width的状态。我对Xamarin Forms很新,所以非常感谢任何帮助。这是一个共享项目,我只是调用.Droid项目来创建AlertDialog,因为它们比共享的DisplayAlert消息更容易定制(增加字体大小等)。

编辑:这是我在Android类中运行的代码:

public async Task<bool> dispAlert(String title, String message, bool tablet, String btn1, String btn2)
    {
        bool val = false;
        Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(Xamarin.Forms.Forms.Context as Activity);
        AlertDialog alert = dialog.Create();

        //alert.SetTitle(title);
        if (tablet)
        {
            //message.Replace("\n", "<br/>");
            //Debug.WriteLine("message = "+message);
            if (Convert.ToInt32(Android.OS.Build.VERSION.Sdk) >= 24)
            {
                alert.SetTitle(Html.FromHtml("<big><big><big>" + title + "</big></big></big>", 0));
            }
            else
            {
                alert.SetTitle(Html.FromHtml("<big><big><big>" + title + "</big></big></big>"));
            }
        }
        else
        {
            alert.SetTitle(title);
        }

        //Debug.WriteLine("Android Build # = " + Android.OS.Build.VERSION.Sdk);

        //need to check for Android version # and if 24 or higher add ,0
        if (tablet)
        {
            message.Replace("\n","<br/>");
            //Debug.WriteLine("message = "+message);
            if (Convert.ToInt32(Android.OS.Build.VERSION.Sdk) >= 24)
            {
                alert.SetMessage(Html.FromHtml("<big><big>" + message + "</big></big>", 0));
            }
            else
            {
                alert.SetMessage(Html.FromHtml("<big><big>" + message + "</big></big>"));
            }
        }else{
            alert.SetMessage(message);
        }


        await Task.Run(() =>
        {
            var waitHandle = new AutoResetEvent(false);

            if (tablet)
            {
                //Debug.WriteLine("set tablet buttons");
                if (Convert.ToInt32(Android.OS.Build.VERSION.Sdk) >= 24)
                {
                    alert.SetButton((int)(DialogButtonType.Positive), Html.FromHtml("<big><big>" + btn1 + "</big></big>",0), (sender, e) =>
                    {
                        val = true;
                        waitHandle.Set();
                    });

                    if (btn2 != null)
                    {
                        alert.SetButton((int)DialogButtonType.Negative, Html.FromHtml("<big><big>" + btn2 + "</big></big>",0), (sender, e) =>
                        {
                            val = false;
                            waitHandle.Set();
                        });                         
                    }
                }else{

                    alert.SetButton((int)(DialogButtonType.Positive), Html.FromHtml("<big><big>" + btn1 + "</big></big>"), (sender, e) =>
                    {
                        val = true;
                        waitHandle.Set();
                    });
                    if (btn2 != null)
                    {
                        //Debug.WriteLine("message = <big><big>" + btn2 + "</big></big>");
                        alert.SetButton((int)DialogButtonType.Negative, Html.FromHtml("<big><big>" + btn2 + "</big></big>"), (sender, e) =>
                        {
                          val = false;
                          waitHandle.Set();
                        });                         
                    }


                }
            }else{
                //Debug.WriteLine("set phone buttons");
                alert.SetButton((int)(DialogButtonType.Positive), btn1, (sender, e) =>
                {
                    val = true;
                    waitHandle.Set();
                });

                if (btn2 != null)
                {
                    alert.SetButton((int)DialogButtonType.Negative, btn2, (sender, e) =>
                    {
                        val = false;
                        waitHandle.Set();
                    });
                }
            }

            Xamarin.Forms.Device.BeginInvokeOnMainThread(new Action(async () =>
            {
                //alert.Window.SetLayout(1200, 1200);
                alert.Show();

            }));
            waitHandle.WaitOne();
        });
        alert.Dispose();

        //alert.Show();

        return val;
    }

这是我正在做的电话:

if (Device.Idiom == TargetIdiom.Tablet)
{
    ret = await DependencyService.Get<IFileHelper>().dispAlert("Measuring Result", "Do you want to save the result?<br/> " + message, true, "Yes", "No");
}
else
{
    ret = await DependencyService.Get<IFileHelper>().dispAlert("Measuring Result", "Do you want to save the result?\n " + message, false, "Yes", "No");
}

0 个答案:

没有答案