自定义警报对话框不变透明

时间:2016-03-04 09:14:45

标签: android android-layout alertdialog android-alertdialog android-drawable

我为我的应用程序制作了两个警告对话框,但它们的行为方式不同。

以下是对话框的代码,其行为符合要求。

    LayoutInflater inflater = getLayoutInflater();
    View SinglePlayerLayout = inflater.inflate(R.layout.layout_single_player_options, null);
    AlertDialog.Builder alertD = new AlertDialog.Builder(this);
    final AlertDialog dialog = alertD.create();
    dialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.alertbackground));
    dialog.setCancelable(true);
    dialog.setView(SinglePlayerLayout);

的Xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/alertbackground">

alertbackground drawable是

<?xml version="1.0" encoding="utf-8"?>

<solid
    android:color="#424a53f7">
    </solid>
<stroke
    android:width="5dp"
    android:color="#000000">
    </stroke>
</shape>

This is the desired output

现在这里的代码没有按预期工作

            LayoutInflater inflater= getLayoutInflater();
        View ResultAlertBox = inflater.inflate(R.layout.layout_game_result_2p_same_device, null);
        ResultAlertBox.setBackgroundResource(R.drawable.alertbackground);

        AlertDialog.Builder alertD = new AlertDialog.Builder(this);
        final AlertDialog alertDialog = alertD.create();
        alertDialog.setView(ResultAlertBox);
        alertDialog.setCancelable(false);
        alertDialog.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.alertbackground));

此对话框的Xml文件是

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

alertdialog drawable对两者都很常见,但显示的输出是

Behind the alert dialog there is a GridView with nine image views on it and above the grid is a relative layout.

请解释相同代码的不同行为。

0 个答案:

没有答案