Android Dim背景的自定义对话框 [英] Android Dim Background of Custom Dialog

查看:101
本文介绍了Android Dim背景的自定义对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我似乎无法调暗我创建的自定义对话框的背景.在线无数解决方案在下面的第一个代码段中提到了最后三行代码,这对对话框的用户界面没有影响.

As titled, I cannot seem to be able to dim the background of the custom dialog box I have made. Countless solutions online mention the last 3 lines of code in the first snippet below, this has made no impact on the UI of the dialog box.

请参见以下代码:

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文件如下:

The layout xml file for the custom dialog is as follows:

<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.

dimAmount is ignored unless this flag is present.

这篇关于Android Dim背景的自定义对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆