AppCompatActivity 改变了我的 AlertDialog 设计

AppCompatActivity has changed my AlertDialog design

我已将 activity 更改为 appCompat Activity。 此外,我将 AlertDialog 更改为 Android.Support.V7.App.AlertDialog。 但是我丢失了以前的警报对话框设计。 这就是它的样子。

这就是现在的样子 到目前为止我的主题是

parent="@android:style/Theme.Holo.Light.DarkActionBar">

但我被迫更改它,因为 appCompat 不支持 Holo 主题。 所以我把它改成

 parent="Theme.AppCompat.Light.DarkActionBar">

如何使警告对话框看起来像以前的对话框?

试试这个:

AlertDialog 对话框= new AlertDialog.Builder(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Dialog));

如果你想让按钮可以帮助用户更容易按下,你可以用自定义按钮构建一个对话框并为按钮设置样式(e.g.style drawable文件夹上的按钮)。你可以参考下面的代码:

 AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        AlertDialog alert = dialog.Create();
        alert.SetTitle("Login Information");
        //alert.SetMessage("Complex Alert");
        //alert.SetIcon(Resource.Drawable.alert);

        LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService);
        View view = inflater.Inflate(Resource.Layout.input_layout, null);
        alert.SetView(view);

        EditText editText_name = view.FindViewById<EditText>(Resource.Id.et_name);
        EditText editText_pwd = view.FindViewById<EditText>(Resource.Id.et_pwd);

        Button button1 = view.FindViewById<Button>(Resource.Id.button1);
        Button button2 = view.FindViewById<Button>(Resource.Id.button2);

        button1.Click += delegate {

            Toast.MakeText(this,"press button1!",ToastLength.Short).Show();
        };

        button2.Click += delegate {
            Toast.MakeText(this, "press button2!", ToastLength.Short).Show();
        };

        //alert.SetButton("OK", (c, ev) =>
        //{
        //    // Ok button click task  
        //    string name = editText_name.Text;
        //    string password = editText_pwd.Text;
        //    Toast.MakeText(this, "name = " + name + " password= " + password, ToastLength.Long).Show();
        //});
        //alert.SetButton2("CANCEL", (c, ev) => { });
        alert.Show();

input_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"  
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText 
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/et_name"
  android:hint="please input name"
/>
<EditText 
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/et_pwd"
  android:password="true"
  android:hint="please input password"
/>

<LinearLayout 
   android:padding="20dp"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  >

    <Button 
    android:id="@+id/button1"
     android:text="button1"
     android:textColor="@android:color/white"
      android:layout_weight="1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/defaultbutton"
    />
    <Button
    android:id="@+id/button2"
    android:layout_marginLeft="20dp"
    android:text="button2"
    android:textColor="@android:color/white"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/defaultbutton"
    />
</LinearLayout>

在文件夹 drawable

中定义一个 xml(例如 defaultbutton.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#1E90FF" />
<!--<stroke
    android:width="2dp"
    android:color="#ffffff" />-->
<corners
  android:bottomLeftRadius="20dp"
  android:bottomRightRadius="20dp"
  android:topLeftRadius="20dp"
  android:topRightRadius="20dp" />
</shape>

注:

1.define文件夹drawable

中的一个xml(defaultbutton.xml)

2.use 像这样:

android:background="@drawable/defaultbutton"

结果是: