具有自定义布局的 AlertDialog 无法正确呈现(添加了边框)

AlertDialog with custom layout doesn't render properly (a border gets added)

我目前在尝试使用 AlertDialog.setView(View v) 函数时遇到问题。

呈现对话框时,无论使用何种主题,它都会在布局周围生成一个边框,就像另一个布局封装它一样。

单独使用 builder.show()、单独使用 builder.create() 或两者一起使用会产生相同的结果。

我是不是遗漏了什么地方,还是一个已知的错误?如果您能提供帮助,请提前致谢。

dialog_status.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <Spinner
        android:id="@+id/spinnerStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <EditText
        android:id="@+id/editTextStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Status"
        android:inputType="textMultiLine"/>

    <CheckBox
        android:id="@+id/checkBoxStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:text="@string/save_status"/>

</LinearLayout>

里面的代码Activity:

private void showStatusDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Dialog);
    //AlertDialog.Builder builder = new AlertDialog.Builder(this); (Yields the same results, just another theme)
    View root = View.inflate(this, R.layout.dialog_status, null);

    final EditText input = (EditText) root.findViewById(R.id.editTextStatus);
    final Spinner spinner = (Spinner) root.findViewById(R.id.spinnerStatus);
    final CheckBox checkBox = (CheckBox) root.findViewById(R.id.checkBoxStatus);
    final Preferences p = new Preferences(FChatActivity.this);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            input.setText(p.getDefaultStatusMessage(FCharacter.Status.getIdentifiers().get(i)));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
    adapter.addAll(FCharacter.Status.getLabels());
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    builder.setView(root)
            .setMessage("Update your status")
            .setTitle("Status")
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    setStatus(FCharacter.Status.getIdentifiers().get(spinner.getSelectedItemPosition()),input.getText().toString(), checkBox.isChecked());
                    dialogInterface.dismiss();
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });

    builder.create();
    builder.show();
}

截图:

好的,我在另一个完全不相关的问题上找到了解决方案 (How to show a Holo (dark) AlertDialog in a themed activity?) :

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog);
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper);

Create a new style in styles.xml like

<style name="Dialog.NoActionBar" parent="Theme.AppCompat.Dialog">
    <item name="android:windowBackground">@color/colorPrimary</item>
    <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Then use the style in your AlertDialog like.

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Dialog_NoActionBar);

希望对您有所帮助。