向警报对话框生成器布局添加自定义边框

Add custom borders to Alert Dialog Builder Layout

我正在尝试向我的警报对话框构建器添加带边框的自定义布局,但这是我得到的结果:

我为对话框创建了样式:

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <item name="android:background">@drawable/dialog_background</item>
</style>

我是这样定义的:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid
    android:color="#00000000" />
<stroke
    android:width="5dp"
    android:background="#00ffffff" />
<corners android:radius="20dp" />

然后,在布局中我也定义了这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_80sdp"
android:gravity="center"
style="@style/MyDialog">

<CheckBox
    android:id="@+id/myCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="@dimen/_10sdp"
    android:text="Abilitare la palette di colori?" />

<EditText
    android:id="@+id/nameEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/genderRadioGroup"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:hint="Inserisci l'eta'">

    <requestFocus />
</EditText>

<RadioGroup
    android:id="@+id/genderRadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/myCheckBox"
    android:checkedButton="@+id/maleRadioButton">


    <RadioButton
        android:id="@+id/maleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Maschio" />


    <RadioButton
        android:id="@+id/femaleRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/maleRadioButton"
        android:layout_alignParentLeft="true"
        android:text="Femmina" />
</RadioGroup>

1) 为什么确定按钮变形了? 2)无法使用getWindow().setBackgroundDrawableResource(android.R.color.transparent);方法,如何使背景透明? 这是警报的 Java 代码:

new AlertDialog.Builder(MainActivity.this).setView(formElementsView)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
                            int selectedId = genderRadioGroup.getCheckedRadioButtonId();
                            RadioButton selectedRadioButton = (RadioButton) formElementsView.findViewById(selectedId);
                            Intent myIntent = new Intent(MainActivity.this, PaintingActivity.class);
                            myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            if (myCheckBox.isChecked()) myIntent.putExtra("palette", "yes");
                            else myIntent.putExtra("palette", "no");
                            myIntent.putExtra("gender", selectedRadioButton.getText());
                            String eta = nameEditText.getText().toString();
                            if (eta.length()!=0) myIntent.putExtra("eta", eta);
                            else myIntent.putExtra("eta", "0");
                            myIntent.putExtra("protocollo", "a");
                            myIntent.putExtra("cornice", "1" + "");
                            myIntent.putExtra("userLogged", userLogged);
                            myIntent.putExtra("first", "yes");
                            MainActivity.this.startActivity(myIntent);
                            dialog.cancel();
                        }

                    }).show().getWindow().setLayout(600, 600);

为什么确定按钮变形了? -

Beacause you have used .setPositiveButton("OK",.. If you want an OK button define it directly on the customized xml file.

如何让背景透明 -

Set a background to the customized layout just like you have created the shape and adding a color to it. Place it in the root layout.

一个相关的例子,

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@android:color/transparent">

<!--Transparent background for outermost layout-->

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/alert_background_xml"
        android:layout_margin="20dp"
        android:layout_centerInParent="true">

<!--alert_background_xml is your customized style -->
    <!--Your views and buttons here including the OK Button-->

    </RelativeLayout>
    </RelativeLayout>

使用 shapeAppearanceOverlay 属性的组件的新 Material Theme you can customize the shape

类似于:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlayAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
  </style>

  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>

您还可以在主题中添加此属性,为应用中的所有对话框定义样式:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light"> 
    ...
    <item name="materialAlertDialogTheme">@style/MyThemeOverlayAlertDialog</item>
    ...
</style>