【问题标题】:Android Dim Background of Custom Dialog自定义对话框的Android暗淡背景
【发布时间】:2014-02-06 20:22:21
【问题描述】:

如标题所示,我似乎无法使我制作的自定义对话框的背景变暗。网上无数解决方案都提到了下面第一个sn-p的最后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>

【问题讨论】:

  • 谢谢,我的代码中已经包含了关于设置暗淡级别的相关信息,(作为参考,第一个 sn-p 中的最后 3 行)。
  • 但是你用过吗:dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); ?
  • 该方法已被弃用。实施时也没有解决问题。
  • 奇怪,我记得,它甚至可以在较新的 android 版本上运行。也许现在它不再起作用了。

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


【解决方案1】:

您是否尝试过添加:

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

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

【讨论】:

  • 是的。我目前只有一个解决方法,我将对话框设置为全屏并将其父视图设置为仅包含半透明 1px * 1px 9 补丁可绘制
  • 是的,这行代码正在运行。我已经添加了 dimAmount 但没有这条线它就无法工作。
猜你喜欢
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多