自定义对话框的Android昏暗背景

标签 android android-xml android-ui android-dialog

如标题所示,我似乎无法调暗我制作的自定义对话框的背景。无数在线解决方案在下面的第一个 fragment 中提到了最后 3 行代码,这对对话框的 UI 没有影响。

请看下面的代码:

Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView textView = (TextView) dialog.findViewById(R.id.textView);
textView.setText("Custom Text Example");
dialog.show();

WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
layoutParams.dimAmount = .7f;
dialog.getWindow().setAttributes(layoutParams);

自定义对话框的布局xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progressDialogCustom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_black"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:text="Updating Profile . . ." />

</LinearLayout>

@drawable/dialog_black文件如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@android:color/background_dark" />

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

    <stroke
        android:width="1px"
        android:color="@android:color/darker_gray" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>

最佳答案

你试过添加:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

dimAmount 将被忽略,除非存在此标志。

关于自定义对话框的Android昏暗背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613238/

相关文章:

android - 跟踪 android 中应用程序的长期 CPU 使用率

java - 从 for 循环中初始化 View

android - 转义空白 Android strings.xml

android - 如何渐变填充按钮的背景?

Android gridview 跳过单元格

android - 在android应用程序中显示html格式的文本

android - 暂停/反转帧动画?

java - Android fragment 代码问题

Android.widget.framelayout 无法转换为 android.support.v4.view.viewpager

java - 在包 'showAsAction' 中找不到属性 'android' 的资源标识符