Android Dim自定义对话框背景

前端之家收集整理的这篇文章主要介绍了Android Dim自定义对话框背景前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
标题是,我似乎无法调暗我所做的自定义对话框的背景.无数解决方案在线提到了下面第一个代码段中的最后3行代码,这对对话框的UI没有任何影响.

请参阅以下代码

  1. Dialog dialog = new Dialog(MainActivity.this);
  2. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
  3. dialog.setContentView(R.layout.dialog);
  4. TextView textView = (TextView) dialog.findViewById(R.id.textView);
  5. textView.setText("Custom Text Example");
  6. dialog.show();
  7.  
  8. WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
  9. layoutParams.dimAmount = .7f;
  10. dialog.getWindow().setAttributes(layoutParams);

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

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/progressDialogCustom"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/dialog_black"
  6. android:orientation="horizontal"
  7. android:padding="10dp" >
  8.  
  9. <ProgressBar
  10. android:id="@+id/progressBar1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content" />
  13.  
  14. <TextView
  15. android:id="@+id/textView"
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:gravity="center_vertical"
  19. android:textColor="@android:color/white"
  20. android:text="Updating Profile . . ." />
  21.  
  22. </LinearLayout>

@ drawable / dialog_black文件如下:

  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:shape="rectangle" >
  3.  
  4. <solid android:color="@android:color/background_dark" />
  5.  
  6. <corners
  7. android:bottomLeftRadius="7dp"
  8. android:bottomRightRadius="7dp"
  9. android:topLeftRadius="7dp"
  10. android:topRightRadius="7dp" />
  11.  
  12. <stroke
  13. android:width="1px"
  14. android:color="@android:color/darker_gray" />
  15.  
  16. <padding
  17. android:bottom="5dp"
  18. android:left="5dp"
  19. android:right="5dp"
  20. android:top="5dp" />
  21.  
  22. </shape>

解决方法

你试过添加
  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

除非存在此标志,否则将忽略dimAmount.

猜你在找的Android相关文章